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-09-12 15:56:42 发布
博客围绕快速排序展开,虽无具体内容,但可推测会涉及快速排序的原理、实现方法等信息技术相关内容。快速排序是重要的排序算法,在数据处理等领域有广泛应用。
58万+

被折叠的 条评论
为什么被折叠?



