public class QuickSort {
public static void main(String[] args) {
// int[] arr = {2, 4, 1, 6, 1, 3, 9};
int[] arr = {2, 2, 2, 1, 1, 1, 1};
quickSort(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
private static void quickSort(int[] arr, int start, int end){
if(start < end){
//找到基准数
int standard = arr[start];
//定义两个指针
int low = start;
int high = end;
//循坏查找比基准小的数字和比基准数大的数
while(low < high){
//如果右边数比基准数大,就指针向前移
while(low < high && arr[high] >= standard){
high--;
}
//把值赋给左边的
arr[low] = arr[high];
//如果左边的比基准值小,指针向后移
while(low < high && arr[low] <= standard){
low++;
}
//把值赋给右边
arr[high] = arr[low];
}
//循坏结束,low和high相等,把基准值赋给他
arr[low] = standard;
//排左边
quickSort(arr,start,low);
//排右边
quickSort(arr,low+1,end);
}
}
}
快速排序
最新推荐文章于 2024-07-17 10:35:12 发布