java快速排序代码实现
时间: 2023-02-18 17:38:09 浏览: 83
这里有一个Java快速排序的例子:
public class QuickSort {
public static void sort(int[] array) {
quickSort(array, 0, array.length - 1);
}
private static void quickSort(int[] array, int start, int end) {
if (start < end) {
int pivot = partition(array, start, end);
quickSort(array, start, pivot - 1);
quickSort(array, pivot + 1, end);
}
}
private static int partition(int[] array, int start, int end) {
int pivot = array[end];
int i = start - 1;
for (int j = start; j < end; j++) {
if (array[j] <= pivot) {
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i + 1];
array[i + 1] = array[end];
array[end] = temp;
return i + 1;
}
}
阅读全文