写一段java快排代码
时间: 2023-02-28 21:17:19 浏览: 92
java编写的快排算法
5星 · 资源好评率100%
好的,这是一段 Java 实现的快速排序代码:
```
import java.util.Arrays;
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
int middle = low + (high - low) / 2;
int pivot = arr[middle];
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
public static void main(String[] args) {
int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
```
希望这段代码对您有所帮助!
阅读全文