快速排序

package lsn1.sjjg.cct.cn.lsn1;

import org.junit.Test;

/**
 * 快速排序
 * Created by linyaokui on 17/12/4.
 */

public class QuickSort {

    @Test
    public void testQuickSort(){
        int[] array=new int[]{1,6,90,86,10};
        quickSort(array,0,array.length-1);

        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
    }

    //快速排序   12  21  31  68  59  40       x=31
    public static void quickSort(int[] array,int begin,int end){
        if(end-begin<1) {return ;}
        int x=array[begin];//31
        int low=begin;//0
        int high=end;//5
        //由于会在两头取数据,需要一个方向
        boolean direction =true;
        //开始进行数据的移动
        L1:
        while(low<high){
            if(direction){//从右往左找
                for(int i=high;i>low;i--){
                    if(array[i]<=x){
                        array[low++]=array[i];
                        high=i;
                        direction=!direction;
                        continue L1;
                    }
                }
                high=low;
            }else{
                for(int i=low;i<high;i++){
                    if(array[i]>=x){
                        array[high--]=array[i];
                        low=i;
                        direction=!direction;
                        continue L1;
                    }
                }
                low=high;
            }
        }
        //把最后找到的值放入中间位置
        array[low]=x;

        //开始完成左右两边的操作
        quickSort(array,begin,low-1);//L
        quickSort(array,low+1,end);//R

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值