package test; public class Test { public static void main(String[] args) { int[] b = sort(new int[]{1,3,44,15,24,26,14,17,19}); for (int g: b){ System.out.print(g+","); } } public static int[] sort(int[] a){ quicksort(a,0,a.length-1); return a; } public static void quicksort(int[] a,int low,int high){ if(low > high){ return; } int i = low ; int j = high; int key = a[low]; while(i < j){ //从右向左 while(i<j && a[j] > key){ j--; } int t = a[j]; a[j] = a[i]; a[i] = t; //从左向右 while(i<j && a[i] <= key){ i++; } if (i<j){ int temp = a[j]; a[j] = a[i]; a[i] = temp; } } //i==j时,替换keyd到a[i] if (j -low > 0) quicksort(a,low,j-1); if (high - j > 0) quicksort(a,i+1,high); } }
快排变种
最新推荐文章于 2022-04-26 17:29:46 发布