zoukankan      html  css  js  c++  java
  • 快速排序个人理解

    
    

      将整个数组分为三个部分,中轴,中轴前部,中轴后部。每一次排序,前一部分<中轴<后一部分。
    然后采用分而治之的思想继续排序。

    其算法如下:

    public static void quickSort(int[] array,int low,int high){
                    if(low>=high){
                        return;
                    }
                    int lowIndex=low;
                    int highIndex=high;
                    int middle=array[lowIndex];
                    while(lowIndex<highIndex){
                        //将第一个数(中轴)与最后一个数进行比较,如果第一个数比最后一个数小,就和其前一个数
                        //进行比较,以此类推。直到找到比第一个数小的值
                        while(lowIndex<highIndex&&middle<array[highIndex]){
                            highIndex--;
                        }
                        //将比第一个数(中轴)大的数放入中轴处。
                        if(lowIndex<highIndex){
                            array[lowIndex++]=array[highIndex];
                        }
                        //经中轴与前一部分刚刚中轴所在处的后一位数进比较,如果比该数大,那么就和该数的后一位数进行比较,
                        //以此类推。直到找到比中轴大的数。
                        while(lowIndex<highIndex&&middle>array[lowIndex]){
                            lowIndex++;
                        }
                        //将比中轴小的数放入刚刚空出来的数那个位置。
                        if(lowIndex<highIndex){
                            array[highIndex--]=array[lowIndex];
                        }
                        array[lowIndex]=middle;
                        quickSort(array, low,lowIndex-1 );
                        quickSort(array, lowIndex+1, high);
                    }
                
            }
  • 相关阅读:
    PHP的错误和异常处理
    PHP 页面编码声明方法详解(header或meta)
    Sentinel实现Redis高可用
    Linux学习系列之Iptables
    Python学习系列之logging模块
    [scrapy]Item Loders
    [scrapy]实例:爬取jobbole页面
    mongo开启验证
    python创建虚拟环境
    elastalert邮件报警
  • 原文地址:https://www.cnblogs.com/orchid9/p/7611866.html
Copyright © 2011-2022 走看看