Java快排演算法
阿新 • • 發佈:2019-02-04
public class QuickSort { public static void quickSortHelp(int[] arr) { quickSort(arr,0, arr.length-1); } public static void quickSort(int[] arr,int low, int high) { if(low<high) { int partition = partition(arr,low,high); quickSort(arr,low, partition-1); quickSort(arr,partition+1, high); } } public static int partition(int[] arr,int low,int high) { while(low<high) { while(arr[high]>=arr[low]&&low<high){ high--; } Swap(arr,high,low); while(arr[low]<=arr[high]&&low<high) { low++; } Swap(arr,high,low); } return low; } public static void Swap(int[] arr,int high,int low) { int temp = arr[low]; arr[low] =arr[high]; arr[high] = temp; } public static void main(String[] args) { int[] array = { 2, 8, 5, 6, 10, 5, 4, 6, 11, 15, 3 }; quickSortHelp(array); for (int s : array) { System.out.println(s); } } }