快速排序java代码
public class QuickSort {
public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, end);
}
}
private static int partition(int[] arr, int begin, int end) {
int pivot = arr[end];
int i = (begin - 1);
for (int j = begin; j < end; j++) {
if (arr[j] <= pivot) {
i++;
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
}
}
int swapTemp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = swapTemp;
return i + 1;
}
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 6, 7, 1, 8, 4};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}
原文地址: https://www.cveoy.top/t/topic/bSn5 著作权归作者所有。请勿转载和采集!