快速排序

来源:互联网 发布:红包软件百度云 编辑:程序博客网 时间:2024/06/06 05:12
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    }}