快速排序算法

package com.hc.sort;

import org.apache.commons.lang.ArrayUtils;

/**
 * Created by congcong.han on 16-8-5 下午6:14.
 */
public class QuickSort {
    public void quickSort(int[] arrays,int start,int stop){
        if(start >= stop ){
            return;
        }
        int middle = arrays[start];
        int middleIndex = start;
        for(int begin  = start+1; begin <= stop; begin++){
            int compare = arrays[begin];
            if(compare < middle){
                middleIndex++;
                move(arrays, begin,start);
            }
        }
        quickSort(arrays,start,middleIndex -1);
        quickSort(arrays,middleIndex+1,stop);
    }

    private void move(int[] arrays, int begin,int start) {
        int value  = arrays[begin];
        for(int i = begin;i > start ;i--){
            arrays[i] = arrays[i-1];
        }
        arrays[start] = value;
    }

    public static void main(String[] args){
        int[] array = new int[]{2,4,1,67,24,899,322,2,323,53,43,5436,3,42,56243};
        new QuickSort().quickSort(array,0,array.length-1);
        System.out.println(ArrayUtils.toString(array));
    }
}

你可能感兴趣的:(算法,java,c++,开发语言)