用Java写快排算法
时间: 2023-02-20 11:35:24 浏览: 30
我可以为你提供一个示例:
public class QuickSort {
public static void quickSort(int[] arr, int start, int end) {
if (start < end) {
int partitionIndex = partition(arr, start, end);
quickSort(arr, start, partitionIndex-1);
quickSort(arr, partitionIndex+1, end);
}
}
private static int partition(int[] arr, int start, int end) {
int pivot = arr[end];
int i = start;
for (int j = start; j < end; j++) {
if (arr[j] <= pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[end];
arr[end] = temp;
return i;
}
}
阅读全文