代码生成Java的快速排序
时间: 2023-03-16 12:43:42 浏览: 61
我可以提供一个快速排序的Java代码示例:
public class QuickSort {
public static void sort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
// 找寻基准数据的正确索引
int index = partition(arr, low, high);
// 进行迭代对index之前和之后的数组进行相同的操作使整个数组变成有序
sort(arr, low, index-1);
sort(arr, index+1, high);
}
private static int partition(int[] arr, int low, int high) {
// 挑选基准值
int pivot = arr[low];
while (low<high) {
while (low<high && arr[high]>=pivot)
high--;
arr[low] = arr[high];
while (low<high && arr[low]<=pivot)
low++;
arr[high] = arr[low];
}
arr[low] = pivot;
return low;
}
}