#include <iostream> #include <iomanip> using namespace std; #include <stdlib.h> #include <time.h> #include <math.h> #include "InsertSort.h" #define random(num) (rand() % (num)) #define randomize() srand((unsigned)time(NULL)) #define N 10000 //排序元素的数目 #define SORT InsertSort //排序方法 class timer//单位ms { public: void start() { start_t = clock(); } clock_t time() { return (clock() - start_t); } private: clock_t start_t; }; int KCN, RMN; timer TIMER; void test(int a[]) { TIMER.start(); SORT<int>(a, N, KCN, RMN); cout << "\tTimeSpared: " << TIMER.time() << "ms" << endl; cout << "KCN=" << left << setw(11) << KCN; cout << "KCN/N=" << left << setw(11)<< (double)KCN/N; cout << "KCN/N^2=" << left << setw(11)<< (double)KCN/N/N; cout << "KCN/NlogN=" << left << setw(11)<< (double)KCN/N/log((double)N)*log(2.0) << endl; cout << "RMN=" << left << setw(11) << RMN; cout << "RMN/N=" << left << setw(11)<< (double)RMN/N; cout << "RMN/N^2=" << left << setw(11)<< (double)RMN/N/N; cout << "RMN/NlogN=" << left << setw(11)<< (double)RMN/N/log((double)N)*log(2.0) << endl; } int main() { int i; //randomize();为了在相同情况下比较各个排序算法,不加这句 int* ascending = new int[N];//升序序列 int* descending = new int[N];//降序序列 int* randomness = new int[N];//随机序列 for (i = 0; i < N; i++) { ascending[i] = i; randomness[i] = i; descending[i] = N - i - 1;} for (i = 0; i < N; i++) swap(randomness[i], randomness[random(N)]); cout << "Sort ascending N=" << N; test(ascending); cout << "Sort randomness N=" << N; test(randomness); cout << "Sort descending N=" << N; test(descending); return 0; } |
需要说明一点,KCN(关键码比较次数)、RMN(记录移动次数)并不是算法必须的,是为了对算法的性能有个直观的评价(不用那些公式算来算去)。对10000个整数排序应该是最省事的测试手段,建议不要再增多记录数目了,一是在最坏的情况不用等太久的时间,二是避免KCN、RMN溢出,另外有些递归的算法在情况比较糟的时候,记录数目太多堆栈可能会溢出,导致程序崩溃。
插入排序
基本思想是,每步将一个待排序的记录,按其关键码大小,插入到前面已经排好序的记录的适当位置,从头做到尾就可以了。
直接插入排序
template <class T> void InsertSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; for (int i = 1; i < N; i++) { T temp = a[i]; RMN++; for (int j = i; j > 0 && ++KCN && temp < a[j - 1]; j--) { a[j] = a[j - 1]; RMN++; } a[j] = temp; RMN++; } } |
template<class T> void InsertSort(T a[], int N) { for (int i = 1; i < N; i++) { T temp = a[i]; for (int j = i; j > 0 && temp < a[j - 1]; j--) a[j] = a[j - 1]; a[j] = temp; } } |
Sort ascending N=10000 TimeSpared: 0ms KCN=9999 KCN/N=0.9999 KCN/N^2=9.999e-005 KCN/NlogN=0.07525 RMN=19998 RMN/N=1.9998 RMN/N^2=0.00019998 RMN/NlogN=0.1505 Sort randomness N=10000 TimeSpared: 330ms KCN=24293730 KCN/N=2429.37 KCN/N^2=0.242937 KCN/NlogN=182.829 RMN=24303739 RMN/N=2430.37 RMN/N^2=0.243037 RMN/NlogN=182.904 Sort descending N=10000 TimeSpared: 711ms KCN=49995000 KCN/N=4999.5 KCN/N^2=0.49995 KCN/NlogN=376.25 RMN=50014998 RMN/N=5001.5 RMN/N^2=0.50015 RMN/NlogN=376.4 |
template<class T> void BinaryInsertSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; for (int i = 1; i < N; i++) { T temp = a[i]; RMN++; int low = 0, high = i - 1; while (low <= high)//折半查找 { int mid = (low + high) / 2; if (++KCN && temp < a[mid]) high = mid - 1; else low = mid + 1; } for (int j = i - 1; j >= low; j--) { a[j + 1] = a[j]; RMN++; }//记录后移 a[low] = temp; RMN++;//插入 } } |
Sort ascending N=10000 TimeSpared: 0ms KCN=123617 KCN/N=12.3617 KCN/N^2=0.00123617 KCN/NlogN=0.930311 RMN=19998 RMN/N=1.9998 RMN/N^2=0.00019998 RMN/NlogN=0.1505 Sort randomness N=10000 TimeSpared: 320ms KCN=118987 KCN/N=11.8987 KCN/N^2=0.00118987 KCN/NlogN=0.895466 RMN=24303739 RMN/N=2430.37 RMN/N^2=0.243037 RMN/NlogN=182.904 Sort descending N=10000 TimeSpared: 631ms KCN=113631 KCN/N=11.3631 KCN/N^2=0.00113631 KCN/NlogN=0.855158 RMN=50014998 RMN/N=5001.5 RMN/N^2=0.50015 RMN/NlogN=376.4 |
template <class T> void TableInsertSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; int* link = new int[N]; int head = 0, pre, cur, i; link[0] = -1; for (i = 1; i < N; i++) { if (a[head] > a[i]) { link[i] = head; head = i; KCN++;}//没设表头,因此需要此判断,失败时此次判断没记入KCN else { for (cur = head; cur != -1&& ++KCN && a[cur] <= a[i]; cur = link[cur]) pre = cur; link[pre] = i; link[i] = cur; } } cur = head;//重排序列 for (i = 0; i < N; i++) { while (cur < i) cur = link[cur]; pre = link[cur]; if (cur != i) { swap(a[i], a[cur]); RMN += 3; link[cur] = link[i]; link[i] = cur; } cur = pre; } delete []link; } |
Sort ascending N=10000 TimeSpared: 751ms KCN=49995000 KCN/N=4999.5 KCN/N^2=0.49995 KCN/NlogN=376.25 RMN=0 RMN/N=0 RMN/N^2=0 RMN/NlogN=0 Sort randomness N=10000 TimeSpared: 621ms KCN=25721250 KCN/N=2572.13 KCN/N^2=0.257213 KCN/NlogN=193.572 RMN=29955 RMN/N=2.9955 RMN/N^2=0.00029955 RMN/NlogN=0.225434 Sort descending N=10000 TimeSpared: 0ms KCN=9999 KCN/N=0.9999 KCN/N^2=9.999e-005 KCN/NlogN=0.07525 RMN=15000 RMN/N=1.5 RMN/N^2=0.00015 RMN/NlogN=0.112886 |
template <class T> void ShellSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; for (int gap = N/2; gap; gap = gap/2) for (int i = gap; i < N; i++) { T temp = a[i]; RMN++; for (int j = i; j >= gap && ++KCN && temp < a[j - gap]; j -= gap) { a[j] = a[j - gap]; RMN++; } a[j] = temp; RMN++; } } |
Sort ascending N=10000 TimeSpared: 0ms KCN=120005 KCN/N=12.0005 KCN/N^2=0.00120005 KCN/NlogN=0.903128 RMN=240010 RMN/N=24.001 RMN/N^2=0.0024001 RMN/NlogN=1.80626 Sort randomness N=10000 TimeSpared: 10ms KCN=258935 KCN/N=25.8935 KCN/N^2=0.00258935 KCN/NlogN=1.94868 RMN=383849 RMN/N=38.3849 RMN/N^2=0.00383849 RMN/NlogN=2.88875 Sort descending N=10000 TimeSpared: 10ms KCN=172578 KCN/N=17.2578 KCN/N^2=0.00172578 KCN/NlogN=1.29878 RMN=302570 RMN/N=30.257 RMN/N^2=0.0030257 RMN/NlogN=2.27707 |
Sort ascending N=100000 TimeSpared: 140ms KCN=1500006 KCN/N=15.0001 KCN/N^2=0.000150001KCN/NlogN=0.903094 RMN=3000012 RMN/N=30.0001 RMN/N^2=0.000300001RMN/NlogN=1.80619 Sort randomness N=100000 TimeSpared: 230ms KCN=4041917 KCN/N=40.4192 KCN/N^2=0.000404192KCN/NlogN=2.43348 RMN=5598883 RMN/N=55.9888 RMN/N^2=0.000559888RMN/NlogN=3.37086 Sort descending N=100000 TimeSpared: 151ms KCN=2244585 KCN/N=22.4459 KCN/N^2=0.000224459KCN/NlogN=1.35137 RMN=3844572 RMN/N=38.4457 RMN/N^2=0.000384457RMN/NlogN=2.31466 |
交换排序
基本思想是:两两比较待排序记录的关键码,如果发生逆序,则交换之,直到所有对象都排好为止。
起泡排序
起泡排序是比较相邻的两个记录,逆序则交换。这样的做法导致小的关键码一层层的浮上来,因此得名。CSDN的论坛曾经讨论过“冒泡”和“起泡”是不是一个东西,看来这是翻译惹的祸,英文名都是Bubble Sort,具体写的时候可以正着排,也可以倒着排。(严版是从后往前排,殷版是从前往后排,好在两本书都翻译为“起泡排序”,不然就正像某些人得出的结论——一个是从后往前排,一个是从前往后排)
template <class T> void BubbleSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; bool exchange = true; for (int i = 1; i < N && exchange; i++) for (int j = N - 1; j >= i; j--) { exchange = false; if (++KCN && a[j - 1] > a[j]) { swap(a[j - 1], a[j]); exchange = true; RMN += 3; } } } |
template <class T> void BubbleSort2(T a[], int N) { for (int i = 0; i < N; i++) for (int j = 1; j < N - i; j++) if (a[j - 1] > a[j]) swap(a[j - 1], a[j]); } |
Sort ascending N=10000 TimeSpared: 0ms KCN=9999 KCN/N=0.9999 KCN/N^2=9.999e-005 KCN/NlogN=0.07525 RMN=0 RMN/N=0 RMN/N^2=0 RMN/NlogN=0 Sort randomness N=10000 TimeSpared: 1161ms KCN=45409094 KCN/N=4540.91 KCN/N^2=0.454091 KCN/NlogN=341.737 RMN=71526984 RMN/N=7152.7 RMN/N^2=0.71527 RMN/NlogN=538.294 Sort descending N=10000 TimeSpared: 1022ms KCN=49995000 KCN/N=4999.5 KCN/N^2=0.49995 KCN/NlogN=376.25 RMN=149985000 RMN/N=14998.5 RMN/N^2=1.49985 RMN/NlogN=1128.75 |
template <class T> int Partition(T a[], int left, int right, int& KCN, int& RMN) { int pivotpos = left; T pivot = a[left];//枢轴 for (int i = left + 1; i <= right; i++) if (++KCN && a[i] < pivot && ++pivotpos != i) { swap(a[i], a[pivotpos]); RMN += 3;} swap(a[left], a[pivotpos]); RMN += 3; return pivotpos; } |
template <class T> void QSRecurve(T a[], int left, int right, int& KCN, int& RMN) { if (left < right) { int pivotpos = Partition<T>(a, left, right, KCN, RMN); QSRecurve<T>(a, left, pivotpos - 1, KCN, RMN); QSRecurve<T>(a, pivotpos + 1, right, KCN, RMN); } } template <class T> void QuickSort(T a[], int N, int& KCN, int& RMN) { KCN = 0; RMN = 0; QSRecurve<T>(a, 0, N - 1, KCN, RMN); } |
Sort ascending N=10000 TimeSpared: 1051ms KCN=49995000 KCN/N=4999.5 KCN/N^2=0.49995 KCN/NlogN=376.25 RMN=29997 RMN/N=2.9997 RMN/N^2=0.00029997 RMN/NlogN=0.22575 Sort randomness N=10000 TimeSpared: 0ms KCN=155655 KCN/N=15.5655 KCN/N^2=0.00155655 KCN/NlogN=1.17142 RMN=211851 RMN/N=21.1851 RMN/N^2=0.00211851 RMN/NlogN=1.59434 Sort descending N=10000 TimeSpared: 1082ms KCN=49995000 KCN/N=4999.5 KCN/N^2=0.49995 KCN/NlogN=376.25 RMN=29997 RMN/N=2.9997 RMN/N^2=0.00029997 RMN/NlogN=0.22575 |
Sort randomness N=100000 TimeSpared: 110ms KCN=2123221 KCN/N=21.2322 KCN/N^2=0.000212322KCN/NlogN=1.27831 RMN=3010848 RMN/N=30.1085 RMN/N^2=0.000301085RMN/NlogN=1.81271 |
template <class T> int Partition(T a[], int left, int right, int& KCN, int& RMN) { int mid = (left + right) / 2; if (++KCN && a[left] > a[mid]) { if (++KCN && a[left] > a[right]) { if (++KCN && a[mid] > a[right]) { swap(a[mid], a[left]); RMN += 3; } else { swap(a[right], a[left]); RMN += 3; } } } else { if (++KCN && a[left] < a[right]) { if (++KCN && a[mid] < a[right]) { swap(a[mid], a[left]); RMN += 3; } else { swap(a[right], a[left]); RMN += 3; } } } int pivotpos = left; T pivot = a[left];//枢轴 for (int i = left + 1; i <= right; i++) if (++KCN && a[i] < pivot && ++pivotpos != i) { swap(a[i], a[pivotpos]); RMN += 3;} swap(a[left], a[pivotpos]); RMN += 3; return pivotpos; } |
Sort ascending N=10000 TimeSpared: 0ms KCN=131343 KCN/N=13.1343 KCN/N^2=0.00131343 KCN/NlogN=0.988455 RMN=35424 RMN/N=3.5424 RMN/N^2=0.00035424 RMN/NlogN=0.266592 Sort randomness N=10000 TimeSpared: 0ms KCN=154680 KCN/N=15.468 KCN/N^2=0.0015468 KCN/NlogN=1.16408 RMN=204093 RMN/N=20.4093 RMN/N^2=0.00204093 RMN/NlogN=1.53595 Sort descending N=10000 TimeSpared: 280ms KCN=12517506 KCN/N=1251.75 KCN/N^2=0.125175 KCN/NlogN=94.2036 RMN=45006 RMN/N=4.5006 RMN/N^2=0.00045006 RMN/NlogN=0.338704 |
Sort ascending N=100000 TimeSpared: 60ms KCN=1665551 KCN/N=16.6555 KCN/N^2=0.000166555KCN/NlogN=1.00276 RMN=393210 RMN/N=3.9321 RMN/N^2=3.9321e-005RMN/NlogN=0.236736 Sort randomness N=100000 TimeSpared: 110ms KCN=1888590 KCN/N=18.8859 KCN/N^2=0.000188859KCN/NlogN=1.13704 RMN=2659857 RMN/N=26.5986 RMN/N^2=0.000265986RMN/NlogN=1.60139 Sort descending N=100000 TimeSpared: 42120ms KCN=1250175006 KCN/N=12501.8 KCN/N^2=0.125018 KCN/NlogN=752.68 RMN=450006 RMN/N=4.50006 RMN/N^2=4.50006e-005RMN/NlogN=0.270931 |
swap(a[left], a[rnd(right-left)+left]); RMN += 3; |
Sort ascending N=100000 TimeSpared: 90ms KCN=1917756 KCN/N=19.1776 KCN/N^2=0.000191776KCN/NlogN=1.1546 RMN=378810 RMN/N=3.7881 RMN/N^2=3.7881e-005RMN/NlogN=0.228066 Sort randomness N=100000 TimeSpared: 120ms KCN=1979189 KCN/N=19.7919 KCN/N^2=0.000197919KCN/NlogN=1.19159 RMN=3175977 RMN/N=31.7598 RMN/N^2=0.000317598RMN/NlogN=1.91213 Sort descending N=100000 TimeSpared: 110ms KCN=2069369 KCN/N=20.6937 KCN/N^2=0.000206937KCN/NlogN=1.24588 RMN=2574174 RMN/N=25.7417 RMN/N^2=0.000257417RMN/NlogN=1.54981 |
int rnd(int n) 选择排序
测试结果:
可以看到KCN固定为n(n-1)/2。另外一件有趣的事是,RMN=0的正序花的时间居然比RMN接近3(n-1)的乱序还多。一是说明测试精度不够,在我的机器上多次测试结果上下浮动10ms是常有的事;二是说明和KCN的n(n-1)/2相比,RMN的3(n-1)有些微不足道。 锦标排序 从直选排序看来,算法的瓶颈在于KCN,而实际上,对于后续的寻找最小值来说,时间复杂度可以降到O(logn)。最为直接的做法是采用锦标赛的办法,将冠军拿走之后,只要把冠军打过的比赛重赛一遍,那么剩下的人中的“冠军”就产生了,而重赛的次数就是竞赛树的深度。实际写的时候,弄不好就会写得很“蠢”,不只多余占用了大量内存,还会导致无用的判断。我没见过让人满意的例程(殷版上的实在太恶心了),自己又写不出来漂亮的,也就不献丑了(其实这是惰性的缘故,有了快排之后,大多数人都不会对其他内排感兴趣,除了基数排序)。实在无聊的时候,不妨写(或者改进)锦标排序来打发时间,^_^。 堆排序 锦标排序的附加储存太多了,而高效的寻找最大值或最小值(O(logn)),我们还有一种方法是堆。这里使用了最大堆,用待排记录的空间充当堆空间,将堆顶的记录(目前最大)和堆的最后一个记录交换,当堆逐渐缩小成1的时候,记录就排序完成了。显而易见的,时间复杂度为O(nlogn),并且没有很糟的情况。
测试结果,直接测试的是N=100000:
整体性能非常不错,附加储存1,还没有很糟的情况,如果实在不放心快排的最坏情况,堆排确实是个好选择。这里仍然出现了KCN、RMN多的反而消耗时间少的例子——误差70ms是不可能的,看来CPU优化的作用还是非常明显的(可能还和Cache有关)。 |