javav写一个快速排序程序
时间: 2023-02-20 22:53:04 浏览: 79
这里是一个快速排序程序的例子:
public class QuickSort {
public static void main(String[] args) {
int[] array = {9, 2, 4, 7, 3, 7, 10};
System.out.println("排序前:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
quickSort(array, 0, array.length - 1);
System.out.println("\n排序后:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void quickSort(int[] array, int low, int high) {
if (array.length == 0)
return;
if (low >= high)
return;
int left = low;
int right = high;
int pivot = array[left];
while (left < right) {
while (left < right && array[right] >= pivot)
right--;
array[left] = array[right];
while (left < right && array[left] <= pivot)
left++;
array[right] = array[left];
}
array[left] = pivot;
quickSort(array, low, left - 1);
quickSort(array, left + 1, high);
}
}
阅读全文