【C++】四种排序算法的时间比较

四种排序算法的时间比较

【注】clock函数对输入(用户输入)元素N排序的计时

  1 #include<iostream>
  2 
  3 #include<time.h>
  4 
  5 using namespace std;
  6 
  7 template<class T>
  8 
  9 inline void Swap(T& a, T& b);
 10 
 11 template<class T>
 12 
 13 void BubbleSort(T a[], int n);
 14 
 15 template<class T>
 16 
 17 void InsertionSort(T a[], int n);
 18 
 19 template<class T>
 20 
 21 void SelectionSort(T a[], int n);
 22 
 23 template<class T>
 24 
 25 void Rank(T a[], int n);
 26 
 27  
 28 
 29  
 30 
 31 int main()
 32 
 33 {
 34 
 35 int n,*a1,*a2,*a3,*a4;
 36 
 37 cout<<"please input a number(1000~60000)"<<endl;
 38 
 39 cin>>n;
 40 
 41 a1=new int[n];
 42 
 43 a2=new int[n];
 44 
 45 a3=new int[n];
 46 
 47 a4=new int[n];
 48 
 49 double start, finish;
 50 
 51 for (int i = 0; i < n; i++)
 52 
 53 { a1[i] = n -i; // initialize
 54 
 55 a2[i]=a1[i];
 56 
 57 a3[i]=a2[i];
 58 
 59 a4[i]=a3[i];
 60 
 61 }
 62 
 63 start = clock( );
 64 
 65 InsertionSort(a1, n);
 66 
 67 finish = clock( );
 68 
 69 cout << n << ' ' << (double)(finish -start) / (CLOCKS_PER_SEC)<< endl;
 70 
 71  
 72 
 73 start = clock( );
 74 
 75 SelectionSort(a2, n);
 76 
 77 finish = clock( );
 78 
 79 cout << n << ' ' << (double)(finish -start) / (CLOCKS_PER_SEC)<< endl;
 80 
 81  
 82 
 83 start = clock( );
 84 
 85 Rank(a3, n);
 86 
 87 finish = clock( );
 88 
 89 cout << n << ' ' << (double)(finish -start) / (CLOCKS_PER_SEC)<< endl;
 90 
 91  
 92 
 93 start = clock( );
 94 
 95 BubbleSort(a4, n);
 96 
 97 finish = clock( );
 98 
 99 cout << n << ' ' << (double)(finish -start) / (CLOCKS_PER_SEC)<< endl;
100 
101  
102 
103  
104 
105 delete []a1;
106 
107 delete []a2;
108 
109 delete []a3;
110 
111 delete []a4;
112 
113 system("pause");
114 
115 }
116 
117  
118 
119 template<class T>
120 
121 inline void Swap(T& a, T& b)
122 
123 {// Swap a and b.
124 
125 T temp = a;
126 
127 a = b;
128 
129 b = temp;
130 
131 }
132 
133 /*********************Bubble Sort*************************/
134 
135 /*进行n- 1次遍历,每次邻位比较,是最大数冒到最后面 */
136 
137 template<class T>
138 
139 void BubbleSort(T a[], int n)
140 
141 {// Sort a[0:n -1] using bubble sort.
142 
143 for (int i = n; i > 1; i--)
144 
145 for (int i = 0; i < n -1; i++)
146 
147 if (a[i] > a[i+1])
148 
149 Swap(a[i], a[i + 1]);
150 
151 }
152 
153 /**********************Insertion Sort***********************/
154 
155 /*每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕*/
156 
157 template<class T>
158 
159 void InsertionSort(T a[], int n)
160 
161 {// Sort a[0:n-1].
162 
163 for (int i = 1; i < n; i++) {
164 
165 // insert a[i] into a[0:i-1]
166 
167 T t = a[i];
168 
169 int j;
170 
171 for (j = i-1; j >= 0 && t < a[j]; j--)
172 
173 a[j+1] = a[j];
174 
175 a[j+1] = t;
176 
177 }
178 
179 }
180 
181 /********************Selection sort************************/
182 
183 /*将最大的数选择出来,并与每次的最后一个数进行交换 */
184 
185 template<class T>
186 
187 void SelectionSort(T a[], int n)
188 
189 {
190 
191 bool sorted = false;
192 
193 for (int size = n; !sorted && (size > 1); size--)
194 
195 {
196 
197 int pos = 0;
198 
199 sorted = true;
200 
201 for (int i = 1; i < size; i++)
202 
203 if (a[pos] <= a[i]) pos = i;
204 
205 else sorted = false; // out of order
206 
207 Swap(a[pos], a[size -1]);
208 
209 }
210 
211 }
212 
213 /*******************Rank sort*****************************/
214 
215 /*先将数组中的元素按大小给它标号,并存在另外一个相应的数组里面,
216 
217 然后新建个数组按照标号读取原来数组的值,之后再把排好后的值依次赋给原来数组
218 
219 */
220 
221 template<class T>
222 
223 void Rank(T a[], int n) {
224 
225 int *r = new int[n+1];
226 
227 for(int i = 0; i < n; i++)
228 
229 r[i] = 0; //initialize
230 
231 for(int i = 1; i < n; i++) {
232 
233 for(int j = 0; j < i; j++) {
234 
235 if(a[j] <= a[i])
236 
237 r[i]++;
238 
239 else r[j]++;
240 
241 }
242 
243 } //end for
244 
245  
246 
247 T *u = new T[n+1];
248 
249 for (int i = 0; i < n; i++) {
250 
251 u[r[i]] = a[i];
252 
253 }
254 
255 for (int i = 0; i < n; i++) {
256 
257 a[i] = u[i];
258 
259 }
260 
261 delete []u;
262 
263 } //end function
264 
265  

 

转载于:https://www.cnblogs.com/tenderwx/p/5246054.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值