当前位置:   article > 正文

C++学习之类的使用(排序算法)_c++然后在类里用方法实现排序

c++然后在类里用方法实现排序

要求:

1) 完成至少3 种排序算法。

2) 排序数据采用*号量化表示。
(即用不同数量的*号表示排序数据,可以参考http://www.sorting-algorithms.com/,需要浏览器允许运行Java Applet)
3) 使用sleep(),system("cls");显示排序过程。

4) 可以通过键盘选择排序算法。

编程要求
1) 类的成员函数、成员函数的定义必须包括private public 访问属性。
2) main 函数中类对象的建立包括两类,类对象,对象指针。
3) 使用new 和delete 建立类对象和释放类对象。
4) 界面设计友好,给出菜单选项,及输入提示。
 扩展要求
尝试对比所有算法对不同排序数据的排序效率(可以以交换数据次数进行对比,也可以通过获取系统处理时间
进行对比),如对已经升序数据、对降序数据、对多组随机数据在不同排序算法的效率进行对比。

初学C++,最开始理解类有点困难,

作为一个大二才学C++的弱菜的我一个排序算法一个类,每个类里面的函数大同小异,可以用继承(省时省力。。)


sort.h

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">#define max 1000
  2. #include<iostream>
  3. #include<windows.h>
  4. #include<stdio.h>
  5. using namespace std;
  6. //int array[max];
  7. int len;
  8. int sign = 0;
  9. class Cocktail
  10. {
  11. private:
  12. int n;
  13. int elem[max];
  14. public:
  15. void print();
  16. void cocktail_sort();//cocktail_sort(int* a, int len)
  17. void copy(int array[max]);
  18. };
  19. class Insert
  20. {
  21. private:
  22. int n;
  23. int elem[max];
  24. public:
  25. void print();
  26. void insert_sort();
  27. void copy(int array[max]);
  28. };
  29. class Qsort
  30. {
  31. private:
  32. int n;
  33. int elem[max];
  34. public:
  35. void print();
  36. void Quick_sort(int first, int high);
  37. void copy(int array[max]);
  38. };
  39. class Bubble
  40. {
  41. private:
  42. int n;
  43. int elem[max];
  44. public:
  45. void print();
  46. void bubble_sort();// bubble_sort(number, SIZE);
  47. void bubble_opo();
  48. void copy(int array[max]);
  49. void sort();
  50. void showans();
  51. };
  52. //冒泡排序
  53. void Bubble::print()
  54. {
  55. for (int i = 0; i < n; i++)
  56. {
  57. for (int j = 1; j <= elem[i]; j++)
  58. {
  59. cout << "*";
  60. }
  61. cout << endl;
  62. }
  63. }
  64. void Bubble::bubble_sort()//n为数组a的元素个数
  65. {
  66. int t = 0;
  67. if (sign == 1)
  68. {
  69. system("cls");
  70. print();
  71. }
  72. int i, j, temp;
  73. for (j = 0; j < n - 1; j++)
  74. {
  75. for (i = 0; i<n - 1 - j; i++)
  76. {
  77. if (elem[i]>elem[i + 1])//数组元素大小按升序排列
  78. {
  79. temp = elem[i];
  80. elem[i] = elem[i + 1];
  81. elem[i + 1] = temp;
  82. t++;
  83. if (sign == 1)
  84. {
  85. Sleep(230);
  86. system("cls");
  87. print();
  88. }
  89. }
  90. }
  91. }
  92. cout << "bubble_sort change times:" << t << endl;
  93. }
  94. void Bubble::bubble_opo()
  95. {
  96. int t = 0;
  97. int i, j, temp;
  98. for (j = 0; j < n - 1; j++)
  99. {
  100. for (i = 0; i < n - 1 - j; i++)
  101. {
  102. if (elem[i] < elem[i + 1])//数组元素大小按升序排列
  103. {
  104. temp = elem[i];
  105. elem[i] = elem[i + 1];
  106. elem[i + 1] = temp;
  107. }
  108. }
  109. }
  110. }
  111. void Bubble::copy(int array[max])
  112. {
  113. int i;
  114. n = len;
  115. for (i = 0; i < n; i++)
  116. {
  117. elem[i] = array[i];
  118. }
  119. }
  120. void Bubble::sort()
  121. {
  122. int i, j, temp;
  123. int t = 0;
  124. for (j = 0; j < n - 1; j++)
  125. {
  126. for (i = 0; i<n - 1 - j; i++)
  127. {
  128. if (elem[i]>elem[i + 1])//数组元素大小按升序排列
  129. {
  130. temp = elem[i];
  131. elem[i] = elem[i + 1];
  132. elem[i + 1] = temp;
  133. }
  134. }
  135. }
  136. }
  137. void Bubble::showans()
  138. {
  139. int i;
  140. cout << endl;
  141. cout << endl;
  142. cout << "排序结果为:" << endl;
  143. for (i = 0; i < n; i++)
  144. {
  145. cout << elem[i] << " ";
  146. }
  147. cout << endl;
  148. cout << endl;
  149. }
  150. //鸡尾酒排序
  151. void Cocktail::print()
  152. {
  153. for (int i = 0; i < n; i++)
  154. {
  155. for (int j = 1; j <= elem[i]; j++)
  156. {
  157. cout << "*";
  158. }
  159. cout << endl;
  160. }
  161. }
  162. void Cocktail::cocktail_sort()
  163. {
  164. int bottom = 0;
  165. int top = n - 1;
  166. int t = 0;
  167. bool swapped = true;
  168. if (sign == 1)
  169. {
  170. system("cls");
  171. print();
  172. }
  173. while (swapped)
  174. {
  175. swapped = false;
  176. for (int i = bottom; i<top; i++)
  177. {
  178. if (elem[i]>elem[i + 1])
  179. {
  180. swap(elem[i], elem[i + 1]);
  181. t++;
  182. swapped = true;
  183. if (sign == 1)
  184. {
  185. Sleep(230);
  186. system("cls");
  187. print();
  188. }
  189. }
  190. }
  191. top = top - 1;
  192. for (int i = top; i > bottom; i--)
  193. {
  194. if (elem[i] < elem[i - 1])
  195. {
  196. swap(elem[i], elem[i - 1]);
  197. t++;
  198. swapped = true;
  199. if (sign == 1)
  200. {
  201. Sleep(230);
  202. system("cls");
  203. print();
  204. }
  205. }
  206. }
  207. }
  208. bottom = bottom + 1;
  209. cout << "cocktail_sort change times:" << t << endl;
  210. }
  211. void Cocktail::copy(int array[max])
  212. {
  213. int i;
  214. n = len;
  215. for (i = 0; i < n; i++)
  216. {
  217. elem[i] = array[i];
  218. }
  219. }
  220. //插入排序
  221. void Insert::print()
  222. {
  223. for (int i = 0; i < n; i++)
  224. {
  225. for (int j = 1; j <= elem[i]; j++)
  226. {
  227. cout << "*";
  228. }
  229. cout << endl;
  230. }
  231. }
  232. void Insert::insert_sort()//int*array, unsigned int n
  233. {
  234. int i, j;
  235. int temp;
  236. int k = 0;
  237. if (sign == 1)
  238. {
  239. system("cls");
  240. print();
  241. }
  242. for (i = 1; i<n; i++)
  243. {
  244. temp = elem[i];
  245. for (j = i; j>0 && elem[j - 1] > temp; j--)
  246. {
  247. elem[j] = elem[j - 1];
  248. k++;
  249. if (sign == 1)
  250. {
  251. Sleep(230);
  252. system("cls");
  253. print();
  254. }
  255. }
  256. elem[j] = temp;
  257. if (sign == 1)
  258. {
  259. Sleep(230);
  260. system("cls");
  261. print();
  262. }
  263. }
  264. cout << "insert_sort change times:" << k << endl;
  265. }
  266. void Insert::copy(int array[max])
  267. {
  268. int i;
  269. n = len;
  270. for (i = 0; i < n; i++)
  271. {
  272. elem[i] = array[i];
  273. }
  274. }
  275. //快速排序
  276. void Qsort::print()
  277. {
  278. for (int i = 0; i < n; i++)
  279. {
  280. for (int j = 1; j <= elem[i]; j++)
  281. {
  282. cout << "*";
  283. }
  284. cout << endl;
  285. }
  286. }
  287. void Qsort::Quick_sort(int low, int high)
  288. {
  289. if (low >= high)
  290. {
  291. return;
  292. }
  293. int first = low;
  294. int last = high;
  295. system("cls");
  296. print();
  297. int key = elem[first];/*用字表的第一个记录作为枢轴*/
  298. while (first < last)
  299. {
  300. while (first < last&&elem[last] >= key)
  301. {
  302. --last;
  303. }
  304. elem[first] = elem[last];/*将比第一个小的移到低端*/
  305. while (first < last&&elem[first] <= key)
  306. {
  307. ++first;
  308. }
  309. elem[last] = elem[first];/*将比第一个大的移到高端*/
  310. if (sign == 1)
  311. {
  312. Sleep(230);
  313. system("cls");
  314. print();
  315. }
  316. }
  317. elem[first] = key;/*枢轴记录到位*/
  318. if (sign == 1)
  319. {
  320. Sleep(230);
  321. system("cls");
  322. print();
  323. }
  324. Quick_sort(low, first - 1);
  325. Quick_sort(first + 1, high);
  326. }
  327. void Qsort::copy(int array[max])
  328. {
  329. int i;
  330. n = len;
  331. for (i = 0; i < n; i++)
  332. {
  333. elem[i] = array[i];
  334. }
  335. }
  336. </span>


main.c

  1. <span style="font-family:Microsoft YaHei;font-size:18px;">//测试数据:80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5
  2. #include<iostream>
  3. #include<windows.h>
  4. #include"Sort.h"
  5. using namespace std;
  6. int main()
  7. {
  8. int array[max];
  9. char temp;
  10. int i;
  11. cout << "//******************请输入数据总数*****************\\" << endl;
  12. cin >> len;
  13. cout << "//**************请输入数据,空格表示间隔!***********\\" << endl;
  14. for (i = 0; i < len; i++)
  15. {
  16. cin >> array[i];
  17. }
  18. cout << "顺序排序->s" << endl;
  19. cout << "逆序排序->r" << endl;
  20. char order;
  21. cin >> order;
  22. Bubble *t=new Bubble();
  23. t->copy(array);
  24. if (order == 'r')
  25. {
  26. t->bubble_opo();
  27. }
  28. else
  29. {
  30. t->sort();
  31. }
  32. t->showans();
  33. delete t;
  34. Bubble *s1 = new Bubble();
  35. Cocktail *s2 = new Cocktail();
  36. Insert *s3 = new Insert();
  37. s1->copy(array);
  38. s1->bubble_sort();
  39. s2->copy(array);
  40. s2->cocktail_sort();
  41. s3->copy(array);
  42. s3->insert_sort();
  43. delete s1;
  44. delete s2;
  45. delete s3;
  46. sign = 1;
  47. while (1)
  48. {
  49. cout << "查看排序过程:" << endl;
  50. cout << "------------a->冒泡排序(Bubble_sort)------------" << endl;
  51. cout << "----------b->鸡尾酒排序(cocktail_sort)----------" << endl;
  52. cout << "------------c->插入排序(insert_sort)------------" << endl;
  53. cout << "-------------d->快速排序(Quick_sort)------------" << endl;
  54. cout << "---------------------e->退出---------------------" << endl;
  55. cin >> temp;
  56. if (temp == 'a')
  57. {
  58. Bubble *s1=new Bubble();
  59. s1->copy(array);
  60. s1->bubble_sort();
  61. delete s1;
  62. }
  63. else if (temp == 'b')
  64. {
  65. Cocktail *s2=new Cocktail();
  66. s2->copy(array);
  67. s2->cocktail_sort();
  68. delete s2;
  69. }
  70. else if (temp == 'c')
  71. {
  72. Insert *s3=new Insert();
  73. s3->copy(array);
  74. s3->insert_sort();
  75. delete s3;
  76. }
  77. else if (temp == 'd')
  78. {
  79. Qsort *s4=new Qsort();
  80. s4->copy(array);
  81. s4->Quick_sort(0,len-1);
  82. delete s4;
  83. }
  84. else if (temp == 'e')
  85. {
  86. system("cls");
  87. cout << "Bye Bye!" << endl;
  88. Sleep(100);
  89. break;
  90. }
  91. }
  92. return 0;
  93. }</span>











声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/926441
推荐阅读
相关标签
  

闽ICP备14008679号