请帮我写一段JAVA的快速排序
时间: 2023-10-18 12:07:01 浏览: 68
java中的快速排序
好的,以下是快速排序的JAVA代码示例:
```
public class QuickSort {
public static void main(String[] args) {
int[] arr = {10, 80, 30, 90, 40, 50, 70};
int size = arr.length;
quickSort(arr, 0, size - 1);
System.out.println("排序后的数组:");
for (int i = 0; i < size; ++i) {
System.out.print(arr[i] + " ");
}
}
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int partitionIndex = partition(arr, left, right);
quickSort(arr, left, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[right];
int i = (left - 1);
for (int j = left; j < right; 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[right];
arr[right] = temp;
return i + 1;
}
}
```
以上就是快速排序的JAVA代码示例。
阅读全文