数据结构拾遗——排序(测试程序)

先总结一下各个排序算法的时间复杂度

排序方法 平均情况 最好情况 最坏情况 辅助空间 稳定性
冒泡 n^2 n n^2 1 稳定
简单选择 n^2 n^2 n^2 1 稳定
直接插入 n^2 n n^2 1 稳定
希尔 nlogn~n^2 n^1.3 n^2 1 不稳定
nlogn nlogn nlogn 1 不稳定
归并 nlogn nlogn nlogn n 稳定
快速 nlogn nlogn n^2 logn~n 不稳定

 

 

 

 

 

 

 

 

发现之前写的排序算法有几个都无法正常工作

写了个测试小程序

现在总算是都正确了

以后可能会测试一下时间

 1 #include "BubbleSort.h"
 2 #include "SelectSort.h"
 3 #include "StraightInsertionSort.h"
 4 #include "ShellSort.h"
 5 #include "MergingSorth.h"
 6 #include "HeapSort.h"
 7 #include "QuickSort.h"
 8 #include <random>
 9 #include <iostream>
10 #include <time.h>
11 using namespace std;
12 
13 void ShowVector(const vector<int> &v) {
14     for (auto i:v)
15     {
16         cout << i << " ";
17     }
18     cout << endl;
19 }
20 
21 void ShowSort(const vector<int> &v) {
22     cout << "unordered:" << endl;
23     cout << "    ";
24     ShowVector(v);
25     vector<int> 
26         vBubble0 = v,
27         vBubble1 = v,
28         vBubble2 = v,
29         vSelect = v,
30         vStraightI = v,
31         vShell = v,
32         vHeap = v,
33         vMerge = v,
34         vQuick = v;
35     BubbleSort0(vBubble0);
36     cout << "BubbleSort0:" << endl;
37     cout << "    ";
38     ShowVector(vBubble0);
39 
40     BubbleSort1(vBubble1);
41     cout << "BubbleSort1:" << endl;
42     cout << "    ";
43     ShowVector(vBubble1);
44 
45     BubbleSort2(vBubble2);
46     cout << "BubbleSort2:" << endl;
47     cout << "    ";
48     ShowVector(vBubble2);
49 
50     SelectSort(vSelect);
51     cout << "SelectSort:" << endl;
52     cout << "    ";
53     ShowVector(vSelect);
54 
55     StraightIS(vStraightI);
56     cout << "StraightIS:" << endl;
57     cout << "    ";
58     ShowVector(vStraightI);
59 
60     ShellSort(vShell);
61     cout << "ShellSort:" << endl;
62     cout << "    ";
63     ShowVector(vShell);
64 
65     HeapSort(vHeap);
66     cout << "HeapSort:" << endl;
67     cout << "    ";
68     ShowVector(vHeap);
69 
70     MergingSort2(vMerge);
71     cout << "MergingSort2:" << endl;
72     cout << "    ";
73     ShowVector(vMerge);
74 
75     QuickSort(vQuick);
76     cout << "QuickSort:" << endl;
77     cout << "    ";
78     ShowVector(vQuick);
79 
80 }
81 
82 void SortCorrect() {
83     uniform_int_distribution<int> rnd(-100, 1000);
84     for (auto i = 0; i < 10; i++)
85     {
86         default_random_engine e((unsigned)time(NULL));
87         vector<int> v;
88         for (auto j = 0; j < 15; j++)
89         {
90             v.push_back(rnd(e));
91         }
92         ShowSort(v);
93         system("pause");
94     }
95 }
96 
97 int main() {
98     SortCorrect();
99 }

 

posted @ 2017-04-18 09:25  r088r088  阅读(253)  评论(1编辑  收藏  举报