快排
来源:互联网 发布:网络侵权行为不包括 编辑:程序博客网 时间:2024/04/27 14:43
package net.itdos.csdn; public class QuickSort { public static void main(String[] args) { int[] nums = {657, 2334, 56, 87, 98, 87, 23, 576, 13, 2}; sort(nums, 0, nums.length-1); for(int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); } } static int partition(int[] nums, int low, int hight) { int pivot = nums[low]; while(low < hight) { while(low < hight && pivot <= nums[hight]) { hight--; } nums[low] = nums[hight]; while(low < hight && pivot >= nums[low]) { low++; } nums[hight] = nums[low]; } nums[low] = pivot; return low; } static void sort(int[] nums, int low, int hight) { if(low < hight) { int pivot = partition(nums, low, hight); sort(nums, low, pivot-1); sort(nums, pivot + 1, hight); } } }