zoukankan      html  css  js  c++  java
  • 快速排序

    public class TestOrder {

    static int[] s = {11 ,3 ,51, 33, 66, 77, 22 ,6};


    public static void main(String[] args) {
    quitSort(s, 0 , 7);
    for (int s :s){
    System.out.println(s);
    }
    }


    /**
    * @Auther sunpz
    * @DateTime 2019-10-30 17:24
    * @Description: 一次排序
    * @Param s 要排序数组
     * @Param l 左边下标
     * @Param r 右边下标
    * @Return: int
    */
    private static int orders(int[] s, int l, int r){
    int i = l, j = r;
    // 取出的中间值
    int x = s[i];

    while (i < j){
    //从右边找到符合要求的-- 小于中间值的
    while (i < j && (x <= s[j])){
    j--;
    }
    //符合条件的放到取 中间值的位置
    if(i < j){
    s[i] = s[j];
    }
    //从左边找到符合要求的-- 大于中间值的
    while (i < j && s[i] <= x){
    i ++;
    }
    //放到填补中间值的位置
    if(i < j){
    s[j] = s[i];
    }
    }
    //最终肯定从x的地方空出一个,将中间值放进去
    s[i] = x;
    return i;
    }

    /**
    * @Auther sunpz
    * @DateTime 2019-10-30 17:59
    * @Description: 分治
    * @Param s
     * @Param l
     * @Param r
    * @Return: void
    */
    private static void quitSort(int s[], int l, int r){
    if(l < r){
    int i = orders(s, l ,r);
    quitSort(s, l, i - 1);
    quitSort(s, i + 1, r);
    }
    }

    }

      

  • 相关阅读:
    iTerm2 颜色配置
    IOS_问题: Xcode8 安装KSImageName插件, 编代码就崩了
    IOS_设置启动图片若干问题
    Android 多国语言
    Reveal 配置与使用
    自定义代码块
    Android 动画
    SQL
    dialog
    2016-1-18UIlabel学习,正则表达式
  • 原文地址:https://www.cnblogs.com/mlfz/p/11766536.html
Copyright © 2011-2022 走看看