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. C# 构造函数的使用方法

    C#构造函数是一个特殊的类方法.在很多方面,包括访问修饰符.重载以及参数列表的语法等方面,构造函数与普通的方法是类似的.然而,在使用方面以及行为方面,构造函数也具有许多特殊的语法和语义规则. 下面列出 ...

  2. unity UGUI动态字体显示模糊

    设置Unity中ttf文件的Character为Unicode,点击apply

  3. HTML5入门1---Canvas画布

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 64位下好神奇啊(增加了PatchGuard技术保护自己,SSDT是相对地址,参数通过寄存器与rdi来传递)

    近期可能会有一个64位平台的驱动开发任务,找了些资料,对64位平台下的驱动开发略知一二了,好神奇. 一.在64位系统下,有一项PatchGuard技术,它是微软为了防止自己的代码被Patch,进而影响 ...

  5. Lambda 表达式型的排序法

    int[] arry = {3,9,5,7,64,51,35,94 }; foreach (int i in arry.OrderBy(i => i)) Console.WriteLine(i) ...

  6. Bolt 动画

    引擎内置的 种动画 --PosChangeAnimation 平移 local ani = XLGetObject("Xunlei.UIEngine.AnimationFactory&quo ...

  7. Topcoder 练习小记,Java 与 Python 分别实现。

    Topcoder上的一道题目,题目描述如下: Problem Statement      Byteland is a city with many skyscrapers, so it's a pe ...

  8. iOS方法封装

    (void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...

  9. 运行Android应用时提示ADB是否存在于指定路径问题

    打开eclipse,选择指定的Android应用工程并Run,提示: [2014-06-28 11:32:26 - LinearLayout] The connectionto adb is down ...

  10. Jquery的.post说解

    Jquery的.post说解(一) 准备工作 ·Customer类   public class Customer {     public int Unid { get; set; }     pu ...