原创

JAVA快速排序算法实现


JAVA快速排序算法实现

private static void quickSort(int[] arr, int start, int end) {
if(start < end) {
int base = arr[start];
int low = start;
int high = end + 1;
while (true) {
while(low < end && arr[++low] <= base)
;
while(high > start && arr[--high] >= base)
;
if (low >= high) {
break;
}
swap(arr, low, high);
}
swap(arr, start, high);
quickSort(arr, start, high - 1);
quickSort(arr, high + 1, end);
}
}

交换方法:

private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}


调用方法:

private static void main(String[] args) {
int[] arr = new int[]{4,7,6,4,5,8,1,8,2,3,54,-1};
int low = 0, high = arr.length - 1;
subSort(arr,low,high);
}




正文到此结束
本文目录