java编写快速排序算法
时间: 2023-09-29 15:06:12 浏览: 38
Java 版快速排序算法如下:
```
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
```
主函数调用方式如下:
```
int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
QuickSort.quickSort(arr, 0, arr.length - 1);
```