



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数据结构作业排序数据结构作业化化排序介绍实现了冒泡排序、插入排序及其几种优化、选择排序、希尔排序、快速排序、归并排序。代码实现|includeincludeincludeincludeincludeincludeincludeincludeincludeincludeincludeincludeusingnamespacestd;Idefinefori(i,a,b)for(inti=(a);i=(b);i_)definemem(a,b)memset(a),(b),sizeof(a);defineall(x)(x).begin(),(x).end()templatevclassT,classT2
2、boolckmax(T&a,T2b)returnaboolckmin(T&a,T2b)returnab?a=b,1:0;”pedeflonglongll;constintmaxn=2e5+8;void_swap(int*a,int*b)inttemp=*a;*a=*b;*b=temp;I冒泡排序voidbubble_sort(int*_begin,int*_end)for(int*p=_begin;p!=_end;p+)for(int*p2=p+1;p2!=_end;p2+)if(*p2*p)_swap(p,p2);I插入排序voidinsertion_sort(int*_begin,int*
3、_end)for(int*p=_begin+1;p_begin;p2-)if(*(p2-1)*p2)_swap(p2-1,p2);elsebreak;I折半插入排序voidbinary_insertion_sort(int*_begin,int*_end)int*l,*r,*mid,temp;for(int*p=_begin+1;p_end;p+)temp=*p;l=_begin,r=p-1;while(l=r)mid=丨+(r-l)/2;if(*midl;p2-)*p2=*(p2-1);*l=temp;2-路插入排序voidtwo_ways_insertion_sort(int*_begin
4、,int*_end)if(_end-_begin2)return;int*arr=(int*)malloc(sizeof(int)*(_end-_begin);int*_first=arr+(_end-_begin-1),*_last=arr,Inum=1,rnum=1;*_first=min(*_begin,*(_begin+1);*_last=max(*_begin,*(_begin+1);for(int*p=_begin+2;p*_last)*(+_last)=*p,lnum+;elseif(*p*_first)*(-irst)=*p,rnum+;elseif(lnumarr;p2-)i
5、f(*p2*(p2-1)_swap(p2,p2-1);lnum+;else*(-_first)=*p;for(int*p2=irst;p2*(p2+1)_swap(p2,p2+1);rnum+;for(int*p=_begin,*p2=arr;p2=_last|_first_last)*(p+)=*(_first+);elseif(_first=arr+(_end-_begin)*(p+)=*(p2+);elseif(*p2*irst)*(p+)=*(_first+);else*(p+)=*(p2+);free(arr);I/希尔排序voidinsertion_sort2(int*_begin
6、,int*_end,intdelta)for(int*p=_begin+delta;p_begin;p2-=delta)if(*(p2-delta)*p2)_swap(p2-delta,p2);elsebreak;voidshell_sort(int*_begin,int*_end)for(intdelta=_end-_begin;delta;delta=1)for(inti=0;idelta;i+)insertion_sort2(_begin+i,_end,delta);I/选择排序voidselect_sort(int*_begin,int*_end)int*temp;for(int*p=
7、_begin;p!=_end;p+)for(int*p2=temp=p;p2_end;p2+)if(*p2=_end)return;inttemp=*_begin;int*p=_begin,*p2=_end-1;while(pp2)while(p=temp)p2-;while(p=temp)p2-Hif(Pp2)_swap(p,p2);P+;while(pp2&*ptemp)p+;if(p=_end)return;int*mid=_begin+(_end-_begin)/2;merge_sort(_begin,mid);merge_sort(mid,_end);int*arr=(int*)ma
8、lloc(sizeof(int)*(_end-_begin);intcnt=0;int*pl=_begin,*pr=mid,*p=arr;while(plmid|pr=_end)*(p+)=*(pl+);elseif(pl=mid)*(p+)=*(pr+);elseif(*pl*pr)*(p+)=*(pl+);else*(p+)=*(pr+);cnt+;for(p=_begin;p_end;p+)*p=*(arr+(p-_begin);free(arr);intamaxn;intmain(void)intn,com;puts(请输入数据个数:);scanf(%d,&n);printf(请输入d
9、个数据:n,n);fori(i,0,n)scanf(%d,&ai);puts(请选择用什么排序:);puts(1.冒泡排序);puts(2.插入排序”);puts(3.折半插入排序”);puts(4.2-路插入排序”);puts(5.希尔排序”);puts(6.选择排序);puts(7.快速排序”);puts(8归并排序”);scanf(%d,&com);if(com=1)bubble_sort(a,a+n);elseif(com=2)insertion_sort(a,a+n);elseif(com=3)binary_insertion_sort(a,a+n);elseif(com=4)two_ways_insertion_sort(a,a+n);elseif(com=5)shell_sort(a,a+n);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 今天考研数学试卷
- 图书馆员专业个人网络研修计划
- 淮阴区期末数学试卷
- 吉大附中孙洪波数学试卷
- 身体浅表昆虫咬伤的护理
- 2025年新网格员协议书
- 2025年新网吧股份协议书
- 尿失禁患者护理查房
- 重症监护中脑卒中的护理实践
- 疼痛管理下的患者安全护理政策
- 护理管理学考试试题与答案
- 鱼类繁殖与发育课件
- 新外研版新教材高中英语选择性必修一全册课文及翻译(中英Word精编)
- GFG涂油机操作控制台用户手册翻译
- 虚云老和尚自述年谱
- 民法案例分析教程(第五版)完整版课件全套ppt教学教程最全电子教案
- FZ∕T 62044-2021 抗菌清洁巾
- 国家电网有限公司十八项电网重大反事故措施(修订版)
- 选矿专业英语词汇
- 西学中试题库及答案
- 水保环保管理组织机构框图(共1页)
评论
0/150
提交评论