快速排序

中午复习了一下排序,参考了书上的算法,又写了一遍,存个档。

package sort;
/**
 * 快速排序
 * @author pingguoliu
 *
 */
public class QuikSort {

    private static int account=1;   // 排序次数计数
    /**
     * 划分
     * @param list  排序数组
     * @param low   低位      下标大小
     * @param high  高位
     */
    private static void Partition(int[] list, int low, int high) {
        int temp = list[low];   //  用于保存中枢对象
        int pivotkey = list[low];   // 排序对象

        int bottom = low;
        int top = high; // 序列的上下界,用于下次划分作为low和high

//      System.out.print("第" + account + "次开始划分 :枢值=" + temp + "\t");
//      Print(list);

        while (low<high) { // 将比中枢值大的移植右侧,小的移植左侧
            while(low<high && list[high] >= pivotkey) high--;   // 从右侧遍历,忽略其中比中枢值大的
                list[low] = list[high];

            while (low<high && list[low] <= pivotkey) low++;    // 从左侧遍历,忽略其中比中枢值小的
                list[high] = list[low]; 
        }

        list[low] = temp;   // 将中枢值放回

        System.out.print("第" + account + "次结束划分 :枢值=" + temp  + "\t");
        Print(list);

        account++;  // 划分次数+1

        // 将划分好的两个区域再次划分
        if (low-1>bottom) {
            Partition(list, bottom, low-1);
        }

        if (low+1<top) {
            Partition(list, low+1, top);
        }
    }

    /**
     * 输出当前队列情况
     * @param list
     */
    private static void Print(int[] list) {
        for(int i : list) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] list = {49, 38, 65, 97, 76, 13, 27};
        int low = 0;
        int high = list.length-1;

        Partition(list, low, high);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值