java实现快速排序,具体代码实现过程如下:
public static void main(String[] args) {
// 示例
int[] arr = {12, 2, 5, 9, 3};
quickSort(arr, 0, arr.length-1);
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void quickSort(int[] arr, int left, int right) {
if(left < right) {
int pivot = sort(arr, left, right);
quickSort(arr, left, pivot-1);
quickSort(arr, pivot+1, right);
}
}
public static int sort(int[] arr, int left, int right) {
int pivot = arr[left];
while(left < right) {
while(left < right && arr[right] >= pivot) {
right--;
}
if(left < right) {
arr[left] = arr[right];
left++;
}
while(left < right && arr[left] <= pivot) {
left++;
}
if(left < right) {
arr[right] = arr[left];
right--;
}
}
arr[left] = pivot;
return left;
}