package 算法; public class QuickSort { public static int boundary(int[] arr, int startIndex, int endIndex){ int standard = arr[startIndex]; int leftIndex = startIndex; int rightIndex = endIndex; while(leftIndex < rightIndex){ while(leftIndex < rightIndex && arr[rightIndex] > standard){ rightIndex--; } while(leftIndex < rightIndex && arr[leftIndex] < standard){ leftIndex++; } int tmp = arr[rightIndex]; arr[rightIndex] = arr[leftIndex]; arr[leftIndex] = tmp; } arr[rightIndex] = standard; return rightIndex; } public static void quickSort(int[] arr,int startIndex, int endIndex){ if(startIndex >= endIndex){ return; } int boundary = boundary(arr,startIndex,endIndex); quickSort(arr,startIndex,boundary-1); quickSort(arr,boundary+1,endIndex); } public static void main(String[] args) { //int[] arr = {8,9,7,6,10,2,3,5,4}; int[] arr = {8,9,7,6}; quickSort(arr,0,3); for(int i : arr){ System.out.print(i+","); } } }