1. class SortNum
  2. {
  3. public:
  4. SortNum();
  5. virtual ~SortNum();
  6. void exchange(int& b,int& c);//交换数据
  7. void listout(int a[],int n);//列出所有
  8. void selectSort(int a[],int n);//选择
  9. void bublbleSort(int a[],int n);//冒泡
  10. void insertSort(int a[],int n);//插入
  11. void baseSort(int a[],int n);//基数
  12. void quickSort(int a[],int n,int left,int right);//快速
  13. void Merge(int *SR, int *TR, int i, int m, int n);//归并
  14. void Msort( int *SR, int *TR1, int s, int t );
  15. void Copy( int *S, int *T, int s, int t );
  16. };

具体实现:

    1. #include "SortNum.h"
    2. #include "iostream.h"
    3. //////////////////////////////////////////////////////////////////////
    4. // Construction/Destruction
    5. //////////////////////////////////////////////////////////////////////
    6. SortNum::SortNum()
    7. {
    8. }
    9. SortNum::~SortNum()
    10. {
    11. }
    12. //交换两个元素
    13. void SortNum::exchange(int& b,int& c)
    14. {
    15. int tem;
    16. tem=b;
    17. b=c;
    18. c=tem;
    19. }
    20. //输出数组所有元素
    21. void SortNum::listout(int a[],int n)
    22. {
    23. for(int i=0;i<n;i++)
    24. cout <<a[i]<<" ";
    25. cout <<endl;
    26. }
    27. //选择排序
    28. void SortNum::selectSort(int a[],int n)
    29. {
    30. for(int i=0;i<n-1;i++)
    31. {
    32. int k=i;
    33. for(int j=i+1;j<n;j++)
    34. if(a[j]<a[k])
    35. k=j;
    36. exchange(a[i],a[k]);
    37. listout(a,n);
    38. }
    39. }
    40. //冒泡排序
    41. void SortNum::bublbleSort(int a[],int n)
    42. {
    43. for(int i=n;i>1;i--)
    44. for(int j=0;j<i-1;j++)
    45. {
    46. if(a[j]>a[j+1])
    47. {
    48. exchange(a[j],a[j+1]);
    49. listout(a,n);
    50. }
    51. }
    52. }
    53. //插入排序
    54. void SortNum::insertSort(int a[],int n)
    55. {
    56. for(int i=1;i<n;i++)//从第二个元素开始
    57. {
    58. int tem=a[i];
    59. int j;
    60. for(j=i-1;j>=0 && tem<a[j];j--)//判断比其小的,因为前面已经排好序列了,所以可以比,然后后退
    61. a[j+1]=a[j];
    62. a[j+1]=tem;//插入
    63. listout(a,n);
    64. }
    65. }
    66. //基数排序
    67. void SortNum::baseSort(int a[],int n)
    68. {
    69. int r=10;//基数为十
    70. int tem=1;
    71. int max=a[0];
    72. for(int i=0;i<n;i++)//找出最大的,以在while中确定结束的时机
    73. {
    74. if(a[i]>max)
    75. max=a[i];
    76. }
    77. while((max%r)/tem !=0)//若最大的运算完为0.则整个基数排序结束
    78. {
    79. for(int i=n;i>1;i--)
    80. for(int j=0;j<i-1;j++)
    81. {
    82. if((a[j]%r)/tem>(a[j+1]%r)/tem)
    83. {
    84. exchange(a[j],a[j+1]);
    85. }
    86. }
    87. listout(a,n);
    88. tem *=10;
    89. r *=10;
    90. }
    91. }
    92. //快速排序
    93. void SortNum::quickSort(int a[],int n,int left,int right)
    94. {
    95. int i,j;
    96. i=left;
    97. j=right;
    98. int middle=a[(left+right)/2];
    99. do
    100. {
    101. while(a[i]<middle && i<right)//在左右找出一对,然后交换
    102. i++;                        //问1:没成对怎么办?只有一个比中间小的,怎么弄?
    103. //知道的吼!!
    104. while(a[j]>middle && j>left)
    105. j--;
    106. if(i<=j)
    107. {
    108. exchange(a[i],a[j]);
    109. i++;
    110. j--;
    111. listout(a,n);//输出有些问题,递归调用中也输出???
    112. }
    113. }while(i<=j);
    114. if(left<j)//递归调用排序左右两边,级级深入
    115. quickSort(a,n,left,j);
    116. if(right>i)
    117. quickSort(a,n,i,right);
    118. }
    119. //归并排序
    120. //二路归并   问题~~无法输出详细过程
    121. void SortNum::Merge(int *SR, int *TR, int i, int m, int n){
    122. // 将有序的SR[i..m]和SR[m+1..n]归并为有序的TR[i..n]
    123. int j = m+1;
    124. int k = i;
    125. for(; i<=m && j<=n; ++k){// 将SR中记录按关键字从小到大地复制到TR中
    126. if (SR[i]<=SR[j]){
    127. TR[k] = SR[i++];
    128. }else{
    129. TR[k] = SR[j++];
    130. }
    131. }
    132. while (i<=m) TR[k++] = SR[i++];   // 将剩余的 SR[i..m] 复制到TR
    133. while (j<=n) TR[k++] = SR[j++];   // 将剩余的 SR[j..n] 复制到TR
    134. }//Merge
    135. void SortNum::Msort( int *SR, int *TR1, int s, int t ){
    136. // 对SR[s..t]进行归并排序,排序后的记录存入TR1[s..t]
    137. if (s==t){
    138. TR1[s] = SR[s];
    139. }else {
    140. int TR2[7];//注:若main中数组改这里一定要改~~~~
    141. int m = (s+t)/2;   // 将 SR[s..t] 平分为 SR[s..m] 和 SR[m+1..t]
    142. Msort(SR,TR2,s,m);  // 递归地将 SR[s..m] 归并为有序的 TR2[s..m]
    143. Msort(SR,TR2,m+1, t); // 递归地将SR[m+1..t]归并为有序的TR2[m+1..t]
    144. Merge(TR2,TR1,s,m,t); // 将TR2[s..m]和TR2[m+1..t] 归并到 TR1[s..t]
    145. Copy(SR,TR1,s,t);
    146. }// else
    147. } // Msort
    148. void SortNum::Copy( int *S, int *T, int s, int t )
    149. {
    150. for(int i=s;i<=t;i++)
    151. S[i]=T[i];
    152. listout(S,7);
    153. }
    154. void main()
    155. {
    156. int a[7]={81,129,655,543,987,26,794};//问题:数组中了length怎么解决
    157. SortNum so;
    158. cout <<"原始数据"<<endl;
    159. so.listout(a,7);
    160. //so.exchange(a[0],a[1]);//测试exchange方法
    161. //so.listout(a,7);
    162. cout <<"选择排序类型:1.选择,2.冒泡,3.插入,4.基数 5.快速 6.归并"<<endl;
    163. int n;
    164. cin >>n;
    165. int b[7];
    166. switch( n)
    167. {
    168. case 1:so.selectSort(a,7);break;
    169. case 2:so.bublbleSort(a,7);break;
    170. case 3:so.insertSort(a,7);break;
    171. case 4:so.baseSort(a,7);break;
    172. case 5:so.quickSort(a,7,0,6);break;
    173. case 6:so.Msort(a,b,0,6);break;
    174. }
    175. }

六种排序的C++实现的更多相关文章

  1. JavaScript之六种排序法

    1.冒泡排序循环的最大值从length递减每次循环只能排好最后一个,然后递减到第一个 function bubbleSort(){ var changedData = new Array(); var ...

  2. 六种排序算法的JavaScript实现以及总结

    最近几天在系统的复习排序算法,之前都没有系统性的学习过,也没有留下过什么笔记,所以很快就忘了,这次好好地学习一下. 首先说明为了减少限制,以下代码通通运行于Node V8引擎而非浏览器,源码在我的Gi ...

  3. 面试中常用的六种排序算法及其Java实现

    常见排序算法的时间复杂度以及稳定性: 1 public class Sort { public static void main(String[] args){ int[] nums=new int[ ...

  4. Python-数据结构-最全六种排序代码实现

    1.冒泡排序 def bubble_sort(alist): """冒泡排序""" n = len(alist) for j in rang ...

  5. python数据结构-最全的六种排序

    1.冒泡排序: 比较相邻的元素,如果第一个比第二个大,那就交换位置 让大的元素跟下一个相邻的元素作比较,如果大于交换位置 对所有元素重复以上步骤(除了最后一个),直到没有任何一个需要作对比 2.选择排 ...

  6. 8种主要排序算法的C#实现

    作者:胖鸟低飞 出处:http://www.cnblogs.com/fatbird/ 简介 排序算法是我们编程中遇到的最多的算法.目前主流的算法有8种. 平均时间复杂度从高到低依次是: 冒泡排序(o( ...

  7. 排序算法的C#实现

    8种主要排序算法的C#实现   新的一年到了,很多园友都辞职要去追求更好的工作环境,我也是其中一个,呵呵! 最近闲暇的时候我开始重温一些常用的算法.老早就买了<算法导论>,一直都没啃下去. ...

  8. leetcode-數組篇

    Remove Element public class Lc27 { public static int removeElement(int[] nums, int val) { if (nums = ...

  9. php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法

    $arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法  *     思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来.  *     比 ...

随机推荐

  1. linux 命令小例

    xargs示例: ls |xargs -i mv {}  /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...

  2. g++ gcc

    编写一个main.cpp,linux 下执行步骤: (1)g++ mian.cpp ./a.out 后缀名是.cpp,编译完成后的可执行文件一般是a.out,也可以自己指定. (2) 编译命令(C): ...

  3. cojs 安科赛斯特 题解报告

    QAQ 从IOI搬了一道题目过来 官方题解貌似理论上没有我的做法优,我交到BZOJ上也跑的飞快 结果自己造了个数据把自己卡成了4s多,真是忧桑的故事 不过貌似原题是交互题,并不能离线 说说我的做法吧 ...

  4. Java学习笔记之:Java简介

    一.引言 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出. ...

  5. ArcEngine中打开各种数据源(WorkSpace)的连接(转)

    ArcEngine中打开各种数据源(WorkSpace)的连接 (SDE.personal/File.ShapeFile.CAD数据.影像图.影像数据集) ArcEngine 可以接受多种数据源.在开 ...

  6. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  7. asp.net开源CMS推荐

    随着网络技术的发展,目前国内CMS的开发商越来越多,各自都有其独特的优势,大家在选择的时候觉得眼花缭乱,不知道选择哪个比较好,我个人认为开源的CMS还是适合我们学习及研究使用,下边就几个国内的asp. ...

  8. 24个有用的PHP类库分享

    目前,PHP是用于Web开发的最流行的脚本语言.你可以在互联网上随手找到关于PHP大量资料,包括文档.教程.工具等等.PHP不仅是一种功能丰富的语言,它还能帮助开发人员轻松地创建更好的网络环境.为了进 ...

  9. ios ableviewcell的动态加载数据,模仿喜马拉雅动态数据加载

    iphone(UITableViewCell)动态加载图片http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Intr ...

  10. mac 下php运行bug

    如下所说bug在window下没有,在mac下存在. mac下的php报如下错误: fopen("data.json") Error: failed to open stream: ...