java实现 快排

快排案例

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));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值