快速排序算法

package sort;

public class QuickSort {

 /*
  * 思想:通过一趟排序将待排的记录分成两部分
  * 其中一部分比关键字均比另一部分的关键字小
  * 分别对这两部分记录继续递归排序
  */
 public static int quickSort(int a[],int low,int high){
        int key=a[low]; // key=10
       
        while(low<high){       // 两边向中间移动,直到low=high
            while(low<high&&a[high]>=key) // 5?10
                high--;
            a[low]=a[high]; // 不满足a[high]>=key执行
            while(low<high&&a[low]<=key)// 5?5
                low++;// +1
            a[high]=a[low];
           
        }
        a[low]=key;// 中间值了 low=high了,关键位置,循环结束
        System.out.println(low+"------------"+high);
        return low;
    }
   
public static void Qsort(int a[],int low,int high){
   
    int pos;
    if(low<high){
        pos=quickSort(a,low,high); // 运用递归再对依次排序进行排序
        Qsort(a,low,pos-1);
        Qsort(a,pos+1,high);
       
    }
}
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a[]={10,9,8,7,6,5,4,3,2,1};
        System.out.println("位置:"+quickSort(a,0,a.length-1));
       
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+"-->");
        }
       
        //--------------------------------------------------------
        System.out.println("-------------------------------/n");
        int aa[]={10,9,8,7,6,5,4,3,2,1,0,0,1};
        Qsort(aa,0,aa.length-1);
        for(int j=0;j<aa.length;j++){
            System.out.print(aa[j]+"--->");
        }
       
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值