zoukankan      html  css  js  c++  java
  • 快速排序实现代码 算法导论7.1 7.2 7.4

    快速排序通常是实际排序中应用最好的选择,因为平均性能很好,且是原址排序,不稳定。

    书上的大部分内容在分析其运行时间,感觉看一下就好了(还是蛮喜欢数学的,可是。。。)

    #include <iostream>
    #include <algorithm>
    #include <random>
    using namespace std;
    
    //实际应用比较多,原址排序
    typedef int index;
    
    index Partition(int *a, index p, index r){
        int i = p - 1, j = p;
        for (; j < r; ++j){         //循环部分,尝试循环不变式的思维方式解题,证明   
            if (a[j] <= a[r]){
                ++i;
                std::swap(a[i], a[j]);//如果使用^必须得判断是否相等
            }
        }
        std::swap(a[i + 1], a[r]);
        return i + 1;
    }
    //随机版本的分割方式
    index Random_Partition(int *a, index p, index r){
        int rand_q = p+rand() % (r - p+1);//没有使用引擎和分布,感觉好烦。
        std::swap(a[rand_q], a[r]);
        return Partition(a, p, r);
    }
    
    //Hoare分割,算法导论7.1
    index Hoare_Partition(int *a, index p, index r){
        int temp = a[p];
        int i = p - 1, j = r + 1;
        while (1){
            do{
                --j;
            } while (temp < a[j]);
            do{
                ++i;
            } while (temp > a[i]);
                if (i < j)
                std::swap(a[i], a[j]);
            else return j;
        }
    }
    
    void HQuick_Sort(int *a, index p, index r){
        if (p >= r)
            return;
        index q = Hoare_Partition(a, p, r);
        HQuick_Sort(a, p, q);
        HQuick_Sort(a, q+1, r);
    }
    
    void Quick_Sort(int *a,index p,index r){
        if (p >= r)
            return;
        index q = Random_Partition(a, p, r);
        Quick_Sort(a, p, q-1);
        Quick_Sort(a, q+1, r);
    }    
    //重复元素版快排算法导论7.2
    pair<index, index> Partition_M(int *a, index p, index r){
        int i = p - 1, j = p;
        int count = 0;
        for (; j < r; ++j){
            if (a[j] <= a[r]){//想法与前面的快排差不多,过程就是记录一下相同的值的个数,
                ++i;          //如果相等的话就交换,不相等的话交换两次。 
                if (a[j] == a[r]||count==0 ){
                    if (a[j] == a[r])
                        ++count;
                    swap(a[i], a[j]);
                }
                else{
                    a[i - count] = a[j];
                    a[j] = a[i];
                    a[i] = a[r];
                }
                
            }
        }
        swap(a[i+1], a[r]);
        return{ i + 1 - count, i + 1 };
    }
    
    
    void Quick_Sort_M(int *a, index p, index r){
        if (p >= r)
            return;
        pair<int,int> q = Partition_M(a, p, r);
        Quick_Sort(a, p, q.first-1);
        Quick_Sort(a, q.second+1, r);
    }
    //算法7.4 较浅栈深度的尾递归实现
    void Re_Qsort(int *a, int p, int r){
        while (p < r){
            int q = Partition(a, p, r);
            if (q - p < r - q){
                Re_Qsort(a, p, q - 1);
                p = q + 1;
            }
            else {
                Re_Qsort(a, q + 1, r);
                    r = q - 1;
            }
        }
    }
    
    
    int main(){
        int a[10] = { 3, 2, 4, 5, 1, 2, 4, 2, 9, 4 };
        Re_Qsort(a, 0, 9);
        for (int i = 0; i < 10; ++i)
            std::cout << a[i]<<" ";
        int b[10] = { 3, 2, 4, 5, 1, 2, 4, 2, 4, 9 };
    
    }
  • 相关阅读:
    bzoj 3238
    bzoj 3473 后缀自动机多字符串的子串处理方法
    bzoj 2998 第k小字串
    bzoj 3672 利用点分治将CDQ分治推广到树型结构上
    bzoj 3671 贪心
    NOIP模拟题——nan
    NOIP模拟题——kun(栈)
    hduP2586——How far away ?
    DP习题
    NOIP模拟题——来自风平浪静的明天
  • 原文地址:https://www.cnblogs.com/Nastukashii/p/4396690.html
Copyright © 2011-2022 走看看