七种排序算法源码

1.【插入排序】

  1. voidInsertSort(int*p,intsize)
  2. {
  3. for(inti=1;i<size;i++)
  4. {
  5. intj=i;
  6. intt=p[i];
  7. for(;j>0&&p[j-1]>t;j--)
  8. p[j]=p[j-1];
  9. p[j]=t;
  10. }
  11. }

2.【选择排序】

  1. voidSelectSort(int*arr,intsize)
  2. {
  3. for(inti=0;i<size;i++)
  4. {
  5. intpsn=i;
  6. for(intj=i;j<size;j++)
  7. {
  8. if(arr[j]<arr[psn])
  9. psn=j;
  10. }
  11. swap(arr[psn],arr[i]);
  12. }
  13. }

3.【冒泡排序】

  1. voidBubbleSort(int*arr,intsize)
  2. {
  3. boolcg=true;
  4. intn=0;
  5. do
  6. {
  7. cg=false;
  8. for(inti=0;i<size-1-n;i++)
  9. {
  10. if(arr[i]>arr[i+1])
  11. {
  12. cg=true;
  13. swap(arr[i],arr[i+1]);
  14. }
  15. }
  16. n++;
  17. }while(cg);
  18. }

4.【快速排序】

  1. voidQuickSort(int*arr,intsize)
  2. {
  3. int*L,*R;
  4. if(size<=1)return;
  5. if(2==size)
  6. {
  7. if(arr[0]>arr[1])
  8. swap(arr[0],arr[1]);
  9. return;
  10. }
  11. L=&arr[1];
  12. R=&arr[size-1];
  13. while(L<R)
  14. {
  15. while(L<R&&*L<*arr)++L;
  16. while(R>arr&&!(*R<*arr))--R;
  17. if(L<R)swap(*R,*L);
  18. }
  19. swap(*R,*arr);
  20. QuickSort(arr,R-arr);
  21. QuickSort(R+1,size-1-(R-arr));
  22. }

5.【堆排序】

  1. voidShift(int*heap,intstart,intend)
  2. {
  3. inttmp=heap[start];
  4. intparent=start;
  5. intchild=parent*2+1;
  6. while(child<=end)
  7. {
  8. if(child<end&&
  9. heap[child]<heap[child+1])
  10. child++;
  11. if(tmp<heap[child])
  12. {
  13. heap[parent]=heap[child];
  14. parent=child;
  15. child=2*parent+1;
  16. }elsebreak;
  17. }
  18. heap[parent]=tmp;
  19. }
  20. voidBuildHeap(int*heap,intsize)
  21. {
  22. for(inti=size/2-1;i>=0;i--)
  23. Shift(heap,i,size-1);
  24. }
  25. voidHeapSort(int*heap,intsize)
  26. {
  27. BuildHeap(heap,size);
  28. for(inti=size-1;i>0;i--)
  29. {
  30. swap(heap[0],heap[i]);
  31. Shift(heap,0,i-1);
  32. }
  33. }

6.【归并排序】

  1. voidMerge(int*arr,intfirst,intmid,intlast)
  2. {
  3. intsize=last-first+1;
  4. int*tmp=newint[size];
  5. intb1=first;
  6. inte1=mid;
  7. intb2=mid+1;
  8. inte2=last;
  9. intk=0;
  10. while(b1<=e1&&b2<=e2)
  11. if(arr[b1]<arr[b2])
  12. tmp[k++]=arr[b1++];
  13. elsetmp[k++]=arr[b2++];
  14. while(b1<=e1)tmp[k++]=arr[b1++];
  15. while(b2<=e2)tmp[k++]=arr[b2++];
  16. for(inti=0;i<size;i++)
  17. arr[first+i]=tmp[i];
  18. deletetmp;
  19. }
  20. voidMergeSort(int*arr,intfirst,intlast)
  21. {
  22. if(first<last)
  23. {
  24. intmid=(first+last)/2;
  25. MergeSort(arr,first,mid);
  26. MergeSort(arr,mid+1,last);
  27. Merge(arr,first,mid,last);
  28. }
  29. }


7.【希尔排序】

  1. voidShellSort(int*p,intsize)
  2. {
  3. intiTemp;
  4. for(intiStep=size/2;iStep>0;iStep=iStep/2){
  5. for(inti=iStep;i<size;++i){
  6. iTemp=p[i];
  7. intj=i;
  8. for(;j>=iStep;j-=iStep){
  9. if(iTemp<p[j-iStep]){
  10. p[j]=p[j-iStep];
  11. }else{
  12. break;
  13. }
  14. }
  15. p[j]=iTemp;
  16. }
  17. }
  18. }

【代码测试】

  1. #include<iostream>
  2. #include<string>
  3. usingnamespacestd;
  4. typedefvoid(*SortFunc)(int*,int,int*,int*);
  5. //插入排序
  6. voidInsertSort(int*p,intsize,int*move_cnt,int*compare_cnt)
  7. {
  8. for(inti=1;i<size;i++)
  9. {
  10. intj=i;
  11. intt=p[i];
  12. ++*move_cnt;
  13. ++*compare_cnt;
  14. for(;j>0&&p[j-1]>t;j--){
  15. ++*compare_cnt;
  16. p[j]=p[j-1];
  17. ++*move_cnt;
  18. }
  19. p[j]=t;
  20. ++*move_cnt;
  21. }
  22. }
  23. //希尔排序
  24. voidShellSort(int*p,intsize,int*move_cnt,int*compare_cnt)
  25. {
  26. intiTemp;
  27. for(intiStep=size/2;iStep>0;iStep=iStep/2)
  28. {
  29. for(inti=iStep;i<size;++i)
  30. {
  31. iTemp=p[i];
  32. ++*move_cnt;
  33. intj=i;
  34. for(;j>=iStep;j-=iStep)
  35. {
  36. ++*compare_cnt;
  37. if(iTemp<p[j-iStep])
  38. {
  39. p[j]=p[j-iStep];
  40. ++*move_cnt;
  41. }
  42. else
  43. {
  44. break;
  45. }
  46. }
  47. p[j]=iTemp;
  48. ++*move_cnt;
  49. }
  50. }
  51. }
  52. //冒泡排序
  53. voidBubbleSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
  54. {
  55. boolcg=true;
  56. intn=0;
  57. do
  58. {
  59. cg=false;
  60. for(inti=0;i<size-1-n;i++)
  61. {
  62. ++*compare_cnt;
  63. if(arr[i]>arr[i+1])
  64. {
  65. cg=true;
  66. swap(arr[i],arr[i+1]);
  67. *move_cnt+=3;
  68. }
  69. }
  70. n++;
  71. }while(cg);
  72. }
  73. //快速排序
  74. voidQuickSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
  75. {
  76. int*L,*R;
  77. if(size<=1)return;
  78. if(2==size)
  79. {
  80. ++*compare_cnt;
  81. if(arr[0]>arr[1]){
  82. swap(arr[0],arr[1]);
  83. *move_cnt+=3;
  84. }
  85. return;
  86. }
  87. L=&arr[1];
  88. R=&arr[size-1];
  89. while(L<R)
  90. {
  91. ++*compare_cnt;
  92. while(L<R&&*L<*arr){
  93. ++*compare_cnt;
  94. ++L;
  95. }
  96. ++*compare_cnt;
  97. while(R>arr&&!(*R<*arr)){
  98. ++*compare_cnt;
  99. --R;
  100. }
  101. ++*compare_cnt;
  102. if(L<R){
  103. swap(*R,*L);
  104. *move_cnt+=3;
  105. }
  106. }
  107. swap(*R,*arr);
  108. *move_cnt+=3;
  109. QuickSort(arr,R-arr,move_cnt,compare_cnt);
  110. QuickSort(R+1,size-1-(R-arr),move_cnt,compare_cnt);
  111. }
  112. //简单选择排序
  113. voidSelectSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
  114. {
  115. for(inti=0;i<size;i++)
  116. {
  117. intpsn=i;
  118. for(intj=i;j<size;j++)
  119. {
  120. ++*compare_cnt;
  121. if(arr[j]<arr[psn]){
  122. psn=j;
  123. }
  124. }
  125. swap(arr[psn],arr[i]);
  126. *move_cnt+=3;
  127. }
  128. }
  129. voidShift(int*heap,intstart,intend,int*move_cnt,int*compare_cnt)
  130. {
  131. inttmp=heap[start];
  132. ++*move_cnt;
  133. intparent=start;
  134. intchild=parent*2+1;
  135. while(child<=end)
  136. {
  137. ++*compare_cnt;
  138. if(child<end&&heap[child]<heap[child+1]){
  139. ++child;
  140. }
  141. ++*compare_cnt;
  142. if(tmp<heap[child])
  143. {
  144. heap[parent]=heap[child];
  145. ++*move_cnt;
  146. parent=child;
  147. child=2*parent+1;
  148. }elsebreak;
  149. }
  150. heap[parent]=tmp;
  151. ++*move_cnt;
  152. }
  153. voidBuildHeap(int*heap,intsize,int*move_cnt,int*compare_cnt)
  154. {
  155. for(inti=size/2-1;i>=0;i--)
  156. Shift(heap,i,size-1,move_cnt,compare_cnt);
  157. }
  158. voidHeapSort(int*heap,intsize,int*move_cnt,int*compare_cnt)
  159. {
  160. BuildHeap(heap,size,move_cnt,compare_cnt);
  161. for(inti=size-1;i>0;i--)
  162. {
  163. swap(heap[0],heap[i]);
  164. Shift(heap,0,i-1,move_cnt,compare_cnt);
  165. }
  166. }
  167. voidMerge(int*arr,intfirst,intmid,intlast,int*move_cnt,int*compare_cnt)
  168. {
  169. intsize=last-first+1;
  170. int*tmp=newint[size];
  171. intb1=first;
  172. inte1=mid;
  173. intb2=mid+1;
  174. inte2=last;
  175. intk=0;
  176. while(b1<=e1&&b2<=e2){
  177. ++*compare_cnt;
  178. if(arr[b1]<arr[b2])
  179. tmp[k++]=arr[b1++];
  180. elsetmp[k++]=arr[b2++];
  181. ++*move_cnt;
  182. }
  183. while(b1<=e1){
  184. tmp[k++]=arr[b1++];
  185. ++*move_cnt;
  186. ++*compare_cnt;
  187. }
  188. while(b2<=e2){
  189. tmp[k++]=arr[b2++];
  190. ++*move_cnt;
  191. ++*compare_cnt;
  192. }
  193. for(inti=0;i<size;i++){
  194. arr[first+i]=tmp[i];
  195. ++*move_cnt;
  196. }
  197. deletetmp;
  198. }
  199. voidMergeSort(int*arr,intsize,int*move_cnt,int*compare_cnt)
  200. {
  201. if(size>1)
  202. {
  203. intmid=size/2;
  204. MergeSort(arr,mid,move_cnt,compare_cnt);
  205. MergeSort(arr+mid,size-mid,move_cnt,compare_cnt);
  206. Merge(arr,0,mid-1,size-1,move_cnt,compare_cnt);
  207. }
  208. }
  209. //输出数组
  210. voidShowArray(int*p,intsize)
  211. {
  212. for(inti=0;i<size;i++)
  213. {
  214. cout<<p[i];
  215. if(i<size-1)
  216. {
  217. cout<<",";
  218. }
  219. }
  220. cout<<endl;
  221. }
  222. voidCopyToTmp(int*arr,int*temp,intlength){
  223. for(inti=0;i<length;++i)
  224. {
  225. temp[i]=arr[i];
  226. }
  227. }
  228. intmain()
  229. {
  230. intarr[4][20]={{-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100},
  231. {100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20},
  232. {99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79},
  233. {0}};
  234. conststringarr_name[3]={"顺序的数组","逆序的数组","随机的数组"};
  235. SortFuncsort_functions[7]={InsertSort,SelectSort,BubbleSort,QuickSort,ShellSort,HeapSort,MergeSort};
  236. conststringsort_func_name[7]={"插入排序","选择排序","冒泡排序","快速排序","希尔排序","堆排序","归并排序"};
  237. intcompare_cnt=0,move_cnt=0;
  238. cout<<"========================================================"<<endl;
  239. for(inti=0;i<7;++i){
  240. cout<<"****"<<sort_func_name[i]<<"****"<<"测试结果如下:"<<endl;
  241. cout<<"========================================================"<<endl;
  242. for(intj=0;j<3;++j){
  243. compare_cnt=0;
  244. move_cnt=0;
  245. CopyToTmp(arr[j],arr[3],20);
  246. cout<<endl<<arr_name[j]<<":";
  247. ShowArray(arr[3],20);
  248. sort_functions[i](arr[3],20,&move_cnt,&compare_cnt);
  249. cout<<sort_func_name[i]<<"后:";
  250. ShowArray(arr[3],20);
  251. cout<<sort_func_name[i]<<"对"<<arr_name[j]<<"排序,共进行"<<compare_cnt<<"次比较和"<<move_cnt<<"次移动"<<endl;
  252. }
  253. cout<<endl<<"========================================================"<<endl;
  254. }
  255. system("pause");
  256. return0;
  257. }


【测试结果】

  1. ========================================================
  2. ****插入排序****测试结果如下:
  3. ========================================================
  4. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  5. 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  6. 插入排序对顺序的数组排序,共进行19次比较和38次移动
  7. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  8. 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  9. 插入排序对逆序的数组排序,共进行209次比较和228次移动
  10. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  11. 插入排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  12. 插入排序对随机的数组排序,共进行132次比较和151次移动
  13. ========================================================
  14. ****选择排序****测试结果如下:
  15. ========================================================
  16. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  17. 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  18. 选择排序对顺序的数组排序,共进行210次比较和60次移动
  19. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  20. 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  21. 选择排序对逆序的数组排序,共进行210次比较和60次移动
  22. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  23. 选择排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  24. 选择排序对随机的数组排序,共进行210次比较和60次移动
  25. ========================================================
  26. ****冒泡排序****测试结果如下:
  27. ========================================================
  28. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  29. 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  30. 冒泡排序对顺序的数组排序,共进行19次比较和0次移动
  31. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  32. 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  33. 冒泡排序对逆序的数组排序,共进行190次比较和570次移动
  34. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  35. 冒泡排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  36. 冒泡排序对随机的数组排序,共进行189次比较和339次移动
  37. ========================================================
  38. ****快速排序****测试结果如下:
  39. ========================================================
  40. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  41. 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  42. 快速排序对顺序的数组排序,共进行244次比较和54次移动
  43. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  44. 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  45. 快速排序对逆序的数组排序,共进行235次比较和57次移动
  46. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  47. 快速排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  48. 快速排序对随机的数组排序,共进行140次比较和54次移动
  49. ========================================================
  50. ****希尔排序****测试结果如下:
  51. ========================================================
  52. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  53. 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  54. 希尔排序对顺序的数组排序,共进行62次比较和124次移动
  55. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  56. 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  57. 希尔排序对逆序的数组排序,共进行80次比较和160次移动
  58. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  59. 希尔排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  60. 希尔排序对随机的数组排序,共进行88次比较和163次移动
  61. ========================================================
  62. ****堆排序****测试结果如下:
  63. ========================================================
  64. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  65. 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  66. 堆排序对顺序的数组排序,共进行126次比较和119次移动
  67. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  68. 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  69. 堆排序对逆序的数组排序,共进行108次比较和101次移动
  70. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  71. 堆排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  72. 堆排序对随机的数组排序,共进行118次比较和108次移动
  73. ========================================================
  74. ****归并排序****测试结果如下:
  75. ========================================================
  76. 顺序的数组:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  77. 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  78. 归并排序对顺序的数组排序,共进行88次比较和176次移动
  79. 逆序的数组:100,99,96,93,89,87,82,79,75,65,62,51,41,24,18,10,0,-10,-15,-20
  80. 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  81. 归并排序对逆序的数组排序,共进行88次比较和176次移动
  82. 随机的数组:99,75,-10,87,41,100,89,65,18,93,0,51,62,10,-20,82,24,96,-15,79
  83. 归并排序后:-20,-15,-10,0,10,18,24,41,51,62,65,75,79,82,87,89,93,96,99,100
  84. 归并排序对随机的数组排序,共进行88次比较和176次移动


原创文章,转载请注明: 转载自IIcyZhao’s Road

本文链接地址:http://blog.youkuaiyun.com/iicy266/article/details/11905851


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值