六种排序的C++实现
- class SortNum
- {
- public:
- SortNum();
- virtual ~SortNum();
- void exchange(int& b,int& c);//交换数据
- void listout(int a[],int n);//列出所有
- void selectSort(int a[],int n);//选择
- void bublbleSort(int a[],int n);//冒泡
- void insertSort(int a[],int n);//插入
- void baseSort(int a[],int n);//基数
- void quickSort(int a[],int n,int left,int right);//快速
- void Merge(int *SR, int *TR, int i, int m, int n);//归并
- void Msort( int *SR, int *TR1, int s, int t );
- void Copy( int *S, int *T, int s, int t );
- };
具体实现:
- #include "SortNum.h"
- #include "iostream.h"
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- SortNum::SortNum()
- {
- }
- SortNum::~SortNum()
- {
- }
- //交换两个元素
- void SortNum::exchange(int& b,int& c)
- {
- int tem;
- tem=b;
- b=c;
- c=tem;
- }
- //输出数组所有元素
- void SortNum::listout(int a[],int n)
- {
- for(int i=0;i<n;i++)
- cout <<a[i]<<" ";
- cout <<endl;
- }
- //选择排序
- void SortNum::selectSort(int a[],int n)
- {
- for(int i=0;i<n-1;i++)
- {
- int k=i;
- for(int j=i+1;j<n;j++)
- if(a[j]<a[k])
- k=j;
- exchange(a[i],a[k]);
- listout(a,n);
- }
- }
- //冒泡排序
- void SortNum::bublbleSort(int a[],int n)
- {
- for(int i=n;i>1;i--)
- for(int j=0;j<i-1;j++)
- {
- if(a[j]>a[j+1])
- {
- exchange(a[j],a[j+1]);
- listout(a,n);
- }
- }
- }
- //插入排序
- void SortNum::insertSort(int a[],int n)
- {
- for(int i=1;i<n;i++)//从第二个元素开始
- {
- int tem=a[i];
- int j;
- for(j=i-1;j>=0 && tem<a[j];j--)//判断比其小的,因为前面已经排好序列了,所以可以比,然后后退
- a[j+1]=a[j];
- a[j+1]=tem;//插入
- listout(a,n);
- }
- }
- //基数排序
- void SortNum::baseSort(int a[],int n)
- {
- int r=10;//基数为十
- int tem=1;
- int max=a[0];
- for(int i=0;i<n;i++)//找出最大的,以在while中确定结束的时机
- {
- if(a[i]>max)
- max=a[i];
- }
- while((max%r)/tem !=0)//若最大的运算完为0.则整个基数排序结束
- {
- for(int i=n;i>1;i--)
- for(int j=0;j<i-1;j++)
- {
- if((a[j]%r)/tem>(a[j+1]%r)/tem)
- {
- exchange(a[j],a[j+1]);
- }
- }
- listout(a,n);
- tem *=10;
- r *=10;
- }
- }
- //快速排序
- void SortNum::quickSort(int a[],int n,int left,int right)
- {
- int i,j;
- i=left;
- j=right;
- int middle=a[(left+right)/2];
- do
- {
- while(a[i]<middle && i<right)//在左右找出一对,然后交换
- i++; //问1:没成对怎么办?只有一个比中间小的,怎么弄?
- //知道的吼!!
- while(a[j]>middle && j>left)
- j--;
- if(i<=j)
- {
- exchange(a[i],a[j]);
- i++;
- j--;
- listout(a,n);//输出有些问题,递归调用中也输出???
- }
- }while(i<=j);
- if(left<j)//递归调用排序左右两边,级级深入
- quickSort(a,n,left,j);
- if(right>i)
- quickSort(a,n,i,right);
- }
- //归并排序
- //二路归并 问题~~无法输出详细过程
- void SortNum::Merge(int *SR, int *TR, int i, int m, int n){
- // 将有序的SR[i..m]和SR[m+1..n]归并为有序的TR[i..n]
- int j = m+1;
- int k = i;
- for(; i<=m && j<=n; ++k){// 将SR中记录按关键字从小到大地复制到TR中
- if (SR[i]<=SR[j]){
- TR[k] = SR[i++];
- }else{
- TR[k] = SR[j++];
- }
- }
- while (i<=m) TR[k++] = SR[i++]; // 将剩余的 SR[i..m] 复制到TR
- while (j<=n) TR[k++] = SR[j++]; // 将剩余的 SR[j..n] 复制到TR
- }//Merge
- void SortNum::Msort( int *SR, int *TR1, int s, int t ){
- // 对SR[s..t]进行归并排序,排序后的记录存入TR1[s..t]
- if (s==t){
- TR1[s] = SR[s];
- }else {
- int TR2[7];//注:若main中数组改这里一定要改~~~~
- int m = (s+t)/2; // 将 SR[s..t] 平分为 SR[s..m] 和 SR[m+1..t]
- Msort(SR,TR2,s,m); // 递归地将 SR[s..m] 归并为有序的 TR2[s..m]
- Msort(SR,TR2,m+1, t); // 递归地将SR[m+1..t]归并为有序的TR2[m+1..t]
- Merge(TR2,TR1,s,m,t); // 将TR2[s..m]和TR2[m+1..t] 归并到 TR1[s..t]
- Copy(SR,TR1,s,t);
- }// else
- } // Msort
- void SortNum::Copy( int *S, int *T, int s, int t )
- {
- for(int i=s;i<=t;i++)
- S[i]=T[i];
- listout(S,7);
- }
- void main()
- {
- int a[7]={81,129,655,543,987,26,794};//问题:数组中了length怎么解决
- SortNum so;
- cout <<"原始数据"<<endl;
- so.listout(a,7);
- //so.exchange(a[0],a[1]);//测试exchange方法
- //so.listout(a,7);
- cout <<"选择排序类型:1.选择,2.冒泡,3.插入,4.基数 5.快速 6.归并"<<endl;
- int n;
- cin >>n;
- int b[7];
- switch( n)
- {
- case 1:so.selectSort(a,7);break;
- case 2:so.bublbleSort(a,7);break;
- case 3:so.insertSort(a,7);break;
- case 4:so.baseSort(a,7);break;
- case 5:so.quickSort(a,7,0,6);break;
- case 6:so.Msort(a,b,0,6);break;
- }
- }
六种排序的C++实现的更多相关文章
- JavaScript之六种排序法
1.冒泡排序循环的最大值从length递减每次循环只能排好最后一个,然后递减到第一个 function bubbleSort(){ var changedData = new Array(); var ...
- 六种排序算法的JavaScript实现以及总结
最近几天在系统的复习排序算法,之前都没有系统性的学习过,也没有留下过什么笔记,所以很快就忘了,这次好好地学习一下. 首先说明为了减少限制,以下代码通通运行于Node V8引擎而非浏览器,源码在我的Gi ...
- 面试中常用的六种排序算法及其Java实现
常见排序算法的时间复杂度以及稳定性: 1 public class Sort { public static void main(String[] args){ int[] nums=new int[ ...
- Python-数据结构-最全六种排序代码实现
1.冒泡排序 def bubble_sort(alist): """冒泡排序""" n = len(alist) for j in rang ...
- python数据结构-最全的六种排序
1.冒泡排序: 比较相邻的元素,如果第一个比第二个大,那就交换位置 让大的元素跟下一个相邻的元素作比较,如果大于交换位置 对所有元素重复以上步骤(除了最后一个),直到没有任何一个需要作对比 2.选择排 ...
- 8种主要排序算法的C#实现
作者:胖鸟低飞 出处:http://www.cnblogs.com/fatbird/ 简介 排序算法是我们编程中遇到的最多的算法.目前主流的算法有8种. 平均时间复杂度从高到低依次是: 冒泡排序(o( ...
- 排序算法的C#实现
8种主要排序算法的C#实现 新的一年到了,很多园友都辞职要去追求更好的工作环境,我也是其中一个,呵呵! 最近闲暇的时候我开始重温一些常用的算法.老早就买了<算法导论>,一直都没啃下去. ...
- leetcode-數組篇
Remove Element public class Lc27 { public static int removeElement(int[] nums, int val) { if (nums = ...
- php六种基础算法:冒泡,选择,插入,快速,归并和希尔排序法
$arr(1,43,54,62,21,66,32,78,36,76,39); 1. 冒泡排序法 * 思路分析:法如其名,就是像冒泡一样,每次从数组当中 冒一个最大的数出来. * 比 ...
随机推荐
- linux 命令小例
xargs示例: ls |xargs -i mv {} /opt find示例: find -mtime +n -name “*.avi” -type f -exec rm {} \; find - ...
- g++ gcc
编写一个main.cpp,linux 下执行步骤: (1)g++ mian.cpp ./a.out 后缀名是.cpp,编译完成后的可执行文件一般是a.out,也可以自己指定. (2) 编译命令(C): ...
- cojs 安科赛斯特 题解报告
QAQ 从IOI搬了一道题目过来 官方题解貌似理论上没有我的做法优,我交到BZOJ上也跑的飞快 结果自己造了个数据把自己卡成了4s多,真是忧桑的故事 不过貌似原题是交互题,并不能离线 说说我的做法吧 ...
- Java学习笔记之:Java简介
一.引言 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出. ...
- ArcEngine中打开各种数据源(WorkSpace)的连接(转)
ArcEngine中打开各种数据源(WorkSpace)的连接 (SDE.personal/File.ShapeFile.CAD数据.影像图.影像数据集) ArcEngine 可以接受多种数据源.在开 ...
- 48. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- asp.net开源CMS推荐
随着网络技术的发展,目前国内CMS的开发商越来越多,各自都有其独特的优势,大家在选择的时候觉得眼花缭乱,不知道选择哪个比较好,我个人认为开源的CMS还是适合我们学习及研究使用,下边就几个国内的asp. ...
- 24个有用的PHP类库分享
目前,PHP是用于Web开发的最流行的脚本语言.你可以在互联网上随手找到关于PHP大量资料,包括文档.教程.工具等等.PHP不仅是一种功能丰富的语言,它还能帮助开发人员轻松地创建更好的网络环境.为了进 ...
- ios ableviewcell的动态加载数据,模仿喜马拉雅动态数据加载
iphone(UITableViewCell)动态加载图片http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Intr ...
- mac 下php运行bug
如下所说bug在window下没有,在mac下存在. mac下的php报如下错误: fopen("data.json") Error: failed to open stream: ...