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

    template <class T>
    static void quickSort(T * pArray, int low, int high)
    {
        if(low >= high)
            return;
    
        int remberlow = low;
        int remberhigh = high;
    
      //原理以第一位的值为标准,将比它小的值都挪到他左边,比它大的值都挪到右边
    //将开始位置腾出,值由base保存,并且base为基准值 T base = pArray[low]; while(low < high) {
         //从右向左找,找到比base小的数,挪到low的位置(之前腾出),现在high位置被腾出,且此位置必定在base的右边
    while(low < high) { if(pArray[high] < base) { pArray[low] = pArray[high]; low++; break; } high--; }
         //从左向右找,找出比base大的值,挪入high的位置(上面腾出),此时low的位置被腾出
    while(low < high) { if(pArray[low] > base) { pArray[high] = pArray[low]; high--; break; } low++; } }
      //最后low必定等于high,且属于腾出位,将之前保存的base放入即可,此时base左边全部比之小,右边全部比之大或相等 pArray[low]
    = base;
      //递归完成整个快速排序 quickSort(pArray, remberlow, low
    - 1); quickSort(pArray, low + 1, remberhigh); }
  • 相关阅读:
    Django动态下载文件
    单反学习
    网页开发
    从一堆数中随机取出一些值的方法
    C/C++使用HTTP协议上传
    django 初级(一) 配置与周边
    c++11
    外观模式
    sqlite
    linux程序设计1
  • 原文地址:https://www.cnblogs.com/hewei2012/p/2743282.html
Copyright © 2011-2022 走看看