数组a的快速排序java代码
时间: 2023-06-01 08:05:02 浏览: 100
以下是快速排序的Java代码:
```
public class QuickSort {
public static void main(String[] args) {
int[] a = {10, 2, 5, 3, 1, 7, 6, 4, 9, 8};
quickSort(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
private static void quickSort(int[] a, int left, int right) {
if (left >= right) return;
int pivot = partition(a, left, right);
quickSort(a, left, pivot - 1);
quickSort(a, pivot + 1, right);
}
private static int partition(int[] a, int left, int right) {
int pivotValue = a[left];
int i = left, j = right;
while (i < j) {
while (i < j && a[j] >= pivotValue) j--;
while (i < j && a[i] <= pivotValue) i++;
if (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[left] = a[i];
a[i] = pivotValue;
return i;
}
}
```
阅读全文