排序方法——快速排序

最近有个前端大神写错了快速排序,所以我这里记录一下

所谓的快速排序就是分治排序的分之中不断递归把数组分成大小两个部分,再继续分成大小两个部分

分的过程中完成整个数组的有序。

交换次数较多,迭代次数少,空间耗费少

代码如下

    public static void FastSort(int[] arr,int low,int high){
        int l=low;
        int h=high;
        int p=arr[low];
        while(l<h){
            while(l<h&&arr[h]>p)
                h--;
            if(l<h){
                int temp=arr[h];
                arr[h]=arr[l];
                arr[l]=temp;
                l++;
            }
            
            while(l<h&&arr[l]<=p)
                l++;
            if(l<h){
                int temp=arr[h];
                arr[h]=arr[l];
                arr[l]=temp;
                h--;
            }
        }
        if(l>low)FastSort(arr,low,l-1);
        if(h<high)FastSort(arr,h+1,high);
    }
原文地址:https://www.cnblogs.com/blackdeng/p/9028543.html