1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package algorithm.innersort;
/** * @author YellowRifle * @projectName DataStructure * @description: TODO * @create 19-7-1上午9:30 */
//采用递归实现 public class QuickSort { public static void main(String[] args) { int[] array = {1,3,1,3,4,6,7,89,2,432,523,421,9,6}; order(array,0,array.length-1); for (int i : array) { System.out.println(i); } }
public static void order(int[] array,int low,int high){ if (low > high){ return; } int i = low; int j = high; int base = array[low]; while (i < j) { while(array[j] >= base && j > i){ j--; } if (i < j) { array[i++] = array[j]; } while (array[i] <= base && j > i) { i++; } if (i < j) { array[j--] = array[i]; } array[i] = base; order(array,low,i-1); order(array,i+1,high); } } }
|