java实现 快排

来源:互联网 发布:linux daemon 命令 编辑:程序博客网 时间:2024/05/29 04:18

快排案例

import java.util.ArrayList;import java.util.Arrays;/** * Created by shiqiang on 2017/11/9. */public class FastSort {    /**     *  排序方法     * @param numberList 需要排序数值数组     * @param low       数组最小的索引坐标     * @param height    数组最大的索引坐标     * @return  每次返回中轴的索引     */    public static int sort(Integer [] numberList, int low, int height)    {        int index = numberList[low];        while (low < height)        {            // 第一步,将所有小于最小索引对应的值,移到这个值的左侧            while (low < height && numberList[height] >= index)            {                height --;            }            numberList[low] = numberList[height];            // 第二步, 将所有大于index的值,移到这个值的右侧            while (low < height && numberList[low] <= index)            {                low ++;            }            numberList[height] = numberList[low];        }        numberList[low] = index;        return low;    }    /**     * 进行递归调用, 每次获得一个中轴索引middle     * @param list  每次需要处理的数组     * @param low   最小索引     * @param height    最大索引     */    public static void execute(Integer [] list, int low,int height)    {        if (low < height)        {            int middle = sort(list, low, height);            execute(list, low, middle -1);            execute(list, middle + 1, height);        }    }    public static void main(String [] args)    {        Integer[] list={34,3,53,2,23,14,14,10};        execute(list, 0, list.length - 1);        System.out.println(Arrays.toString(list));    }}
原创粉丝点击