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

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

排序方法平均情况最好情况最坏情况辅助空间稳定性
冒泡n^2nn^21稳定
简单选择n^2n^2n^21稳定
直接插入n^2nn^21稳定
希尔nlogn~n^2n^1.3n^21不稳定
nlognnlognnlogn1不稳定
归并nlognnlognnlognn稳定
快速nlognnlognn^2logn~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 }

 

转载于:https://www.cnblogs.com/linhaowei0389/p/6726172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值