排序集锦——有平凡,更有非凡

· · 算法·理论

不定时更新。

摘要

本文章将介绍几种特殊的方法进行排序,并以笔者自己的测试器、模板题作测试,并与其他的平凡算法作比较。 算法仅供娱乐,~如果~没有实际意义~将~不被用于实践。 :::epigraph[——ChrisH_cpp11] 没有 std::sort 的速度,没有普通快排求第 k 小、归并排序求逆序对的能力,活着有啥用。 :::

前情提要

笔者不考虑不基于比较的排序。

笔者使用的电脑是十年老爷机,效率差成了依托。

笔者的测试器:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <queue>
using namespace std;
int a[10000005];//待排序的数组
int n;//待排序的长度
int b[10000005];//辅助排序用的数组
void BubbleSort(){//冒泡排序
    bool flag;
    for (int i = n;i > 1;i--){
        flag = true;
        for (int j = 1;j < i;j++)
            if (a[j] > a[j + 1]){
                swap(a[j],a[j + 1]);
                flag = false;
            }
        if (flag) break;
    }
}
void SelectionSort(){//选择排序
    for (int i = 1;i < n;i++){
        int pos = i;
        for (int j = i;j <= n;j++)
            if (a[pos] > a[j])
                pos = j;
        swap(a[i],a[pos]);
    }
}
void InsertionSort(){//插入排序
    for (int i = 2;i <= n;i++){
        int j = i;
        while (a[j - 1] > a[j] && j > 1){
            swap(a[j - 1],a[j]);
            j--;
        }
    }
}
void stdSort(){//std::sort
    sort(a + 1,a + n + 1);
} 
void QuickSortMPV(int l,int r){//快速排序(以中间项作为基准值)
    if (l < r){
        int mid = l + r >> 1,pivot = a[mid];
        int i = l,j = r;
        while (i <= j){
            while (i <= r && a[i] < pivot)i++;
            while (l <= j && pivot < a[j])j--;
            if (i <= j) swap(a[i++],a[j--]);
        }
        if(i < r) QuickSortMPV(i,r);
        if(l < j) QuickSortMPV(l,j);
    }
}
void QuickSortMPV_break(int l,int r,int breaknum){//快速排序(以中间项作为基准值)但是在待排序区间过短时结束
    if (r - l > breaknum){
        int mid = l + r >> 1,pivot = a[mid];
        int i = l,j = r;
        while (i <= j){
            while (i <= r && a[i] < pivot)i++;
            while (l <= j && pivot < a[j])j--;
            if (i <= j) swap(a[i++],a[j--]);
        }
        if(i < r) QuickSortMPV_break(i,r,breaknum);
        if(l < j) QuickSortMPV_break(l,j,breaknum);
    }
}
void fusionQI(int breaknum){//快速排序带阈值+插入排序混合
    QuickSortMPV_break(1,n,breaknum);
    InsertionSort();
}
void MergeSort(int l,int r){//归并排序
    if (l < r){
        int mid = l + r >> 1;
        MergeSort(l,mid);
        MergeSort(mid + 1,r);
        int i = l,j = mid + 1,cur = l - 1;
        while (i <= mid && j <= r)
            if (a[i] < a[j]) b[++cur] = a[i++];
            else b[++cur] = a[j++];
        while (i <= mid) b[++cur] = a[i++];
        while (j <= r) b[++cur] = a[j++];
        for (i = l;i <= r;i++) a[i] = b[i];
    }
}
void STLHeapSort(){//std::priority_queue排序
    priority_queue<int,vector<int>,greater<int> >pq;
    for (int i = 1;i <= n;i++) pq.push(a[i]);
    for (int i = 1;i <= n;i++){
        a[i] = pq.top();
        pq.pop();
    }
}
int tot;
void goingDown(int pos){
    if (pos << 1 > tot) return ;
    int best = pos << 1;
    if ((pos << 1) + 1 <= tot && a[pos << 1] < a[(pos << 1) + 1]) best++;
    if (a[pos] < a[best]){
        swap(a[best],a[pos]);
        goingDown(best);
    }
}
void pop(){
    swap(a[1],a[tot--]);
    goingDown(1);
}
void MyHeap_Sort(){//堆排序(包括以上三个函数与变量tot)
    tot = n;
    for (int i = n / 2;i >= 1;i--) goingDown(i);
    while (tot) pop();
}
int main(){
    n = 10000000;
    srand(time(0)); 
    for (int i = 1;i <= n;i++){
        a[i] = rand() << 15;
        srand(time(0) + rand());
        a[i] += rand();
        srand(time(0) + rand());
    //a[i] = 1;
    }
    double begin = clock();
    //在这里选择排序算法
    MyHeap_Sort();
    cout << clock() - begin << "ms" << endl;
    //for (int i = 1;i <= n;i++) cout << a[i] << " \n"[i == n];
    return 0;
}
//单位:ms 
//待排序数个数未说明时默认5*10^4,随机 
//BubbleSort:900~1300
//SelectionSort:640
//InsertionSort:860 
//std::sort:15
//std::sort(10^7个数):180~300
//std::sort(10^7个数正序):100~130
//QuickSortMPV:0~15
//QuickSortMPV(10^7个数):220~440
//QuickSortMPV(10^7个数正序):100~130
//QuickSortMPV100break+Insertion(10^7个数):300~350
//MergeSort:0
//MergeSort(10^7个数):420~660
//MergeSort(10^7个数正序):360~375
//std::priority_queueSort:0~15
//std::priority_queueSort(10^7个数):810~2000
//std::priority_queueSort(10^7个数正序):830
//MyHeapSort:0~16
//MyHeapSort(10^7个数):671~1373
//MyHeapSort(正序):880

以上算法的测试记录:冒泡排序、选择排序、插入排序、std::sort、手写快速排序以中间项作为基准值、手写快速排序以中间项作为基准值带结束阈值并在完全结束后使用插入排序 ~(什么长难句)~、归并排序、把所有数据扔进std::priority_queue里再倒出来、手写堆排序。粗体链接是 AC 链接,作为本文的主要对照组。

目录

  1. 混堆排序

    正文

    混堆排序

    注意到小根堆具有父亲值比所有的儿子值都小的特点,那么为什么不直接把小根堆排序呢?

于是我有了以下补充代码: