快速排序~

练练手按照思路能写出来就可以了!!!
 public static void main(String args[]) {
        System.out.println("Hello World!");
        //
        int[] data = {0,5,4,8,9,3,2,67,23};
        sort(data,0,data.length-1);

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

    public static void sort(int[] data,int start,int end){
        //int[] data = {0,5,4,8,9,3,2,67,23};
        //int len = data.length;

        //int left =0,right=len-1;

        int left = start,right = end;
        if(left >= right) return;

        int base_val = data[left];

        while(right >left){
            while(right > left) {
                if (data[right] < base_val) {
                    break;
                }
                right--;
            }

            while(left < right){
                if(data[left] > base_val){
                    break;
                }
                left++;
            }

            if(right==left){
                if(data[left] < data[start]){
                    int tmp = data[left];
                    data[left] = data[start];
                    data[start] = tmp;
                }
            }else {
                int tmp = data[left];
                data[left] = data[right];
                data[right] = tmp;
            }
        }

        sort(data,start,left);
        sort(data,left+1,end);
    }

快速排序(三种方法实现)_小朱同学..的博客-CSDN博客

你可能感兴趣的:(算法相关,java,算法,数据结构)