快速排序算法(Java)

快速排序算法的基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另外一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。

class PartitionSort{
    public void p(int[] a){               //打印输出结果
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+" ");
        }
        System.out.println();
    }
    public int Partition(int[] a,int low,int high){        //找出中间位置的序号,并返回
        int temp = a[low];       //set the low number is middle number
        while(low<high){ 
            while(low<high&&temp<=a[high]){ //this is can not lost =,else is die loop
                high--;                    //high position - 1 
            }
            a[low]=a[high];               //< middle number ,make it in front of middle number 
            while(low<high&&temp>a[low]){ 
                low++;
            }
            a[high]=a[low];
        }
        a[low] = temp;
        return low;
    }
    public void QSort(int[] a,int low,int high){ //recurrence method to come true  使用递归方法进行排序
        if(a.length>0){
            if(low<high){
                int partition = Partition(a,low,high);
                QSort(a,low,partition-1);
                QSort(a,partition+1,high);
            }
        }
    }
    public static void main(String args[]){
        int[] a = {38,49,65,97,76,13,27,49};
        PartitionSort P = new PartitionSort();
        P.QSort(a,0,a.length-1);
        P.p(a);
    }
}

 

转载于:https://www.cnblogs.com/zrlhss/p/4027398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值