09-排序3 Insertion or Heap Sort
2016-05-05 11:30
627 查看
和前一题差不多,把归并排序换成了堆排序。要点还是每一次排序进行判断
开始犯了个错误堆排序该用origin2结果一直在排序origin,误导了半天以为是逻辑错误。。。一直在检查逻辑
建立最大堆
排序并调整下滤
AccordingtoWikipedia:
Insertionsortiterates,consumingoneinputelementeachrepetition,andgrowingasortedoutputlist.Eachiteration,insertionsortremovesoneelementfromtheinputdata,findsthelocationitbelongswithinthesortedlist,andinsertsitthere.Itrepeatsuntilnoinputelementsremain.
Heapsortdividesitsinputintoasortedandanunsortedregion,andititerativelyshrinkstheunsortedregionbyextractingthelargestelementandmovingthattothesortedregion.itinvolvestheuseofaheapdatastructureratherthanalinear-timesearchtofindthemaximum.
Nowgiventheinitialsequenceofintegers,togetherwithasequencewhichisaresultofseveraliterationsofsomesortingmethod,canyoutellwhichsortingmethodweareusing?
开始犯了个错误堆排序该用origin2结果一直在排序origin,误导了半天以为是逻辑错误。。。一直在检查逻辑
建立最大堆
排序并调整下滤
AccordingtoWikipedia:
Insertionsortiterates,consumingoneinputelementeachrepetition,andgrowingasortedoutputlist.Eachiteration,insertionsortremovesoneelementfromtheinputdata,findsthelocationitbelongswithinthesortedlist,andinsertsitthere.Itrepeatsuntilnoinputelementsremain.
Heapsortdividesitsinputintoasortedandanunsortedregion,andititerativelyshrinkstheunsortedregionbyextractingthelargestelementandmovingthattothesortedregion.itinvolvestheuseofaheapdatastructureratherthanalinear-timesearchtofindthemaximum.
Nowgiventheinitialsequenceofintegers,togetherwithasequencewhichisaresultofseveraliterationsofsomesortingmethod,canyoutellwhichsortingmethodweareusing?
InputSpecification:
Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveintegerN(≤100).Theninthenextline,NNintegersaregivenastheinitialsequence.ThelastlinecontainsthepartiallysortedsequenceoftheNnumbers.Itisassumedthatthetargetsequenceisalwaysascending.Allthenumbersinalineareseparatedbyaspace.OutputSpecification:
Foreachtestcase,printinthefirstlineeither"InsertionSort"or"HeapSort"toindicatethemethodusedtoobtainthepartialresult.Thenrunthismethodforonemoreiterationandoutputinthesecondlinetheresulingsequence.Itisguaranteedthattheanswerisuniqueforeachtestcase.Allthenumbersinalinemustbeseparatedbyaspace,andtheremustbenoextraspaceattheendoftheline.SampleInput1:
10 3128759460 1237859460
SampleOutput1:
InsertionSort 1235789460
SampleInput2:
10 3128759460 6451032789
SampleOutput2:
HeapSort 5431026789
#include<iostream> #include<cstdio> #include<cstdlib> usingnamespacestd; typedefintElementType; boolJudge(intorigin[],intchanged[],intlen) { for(inti=0;i<len;i++){ if(origin[i]!=changed[i]) returnfalse; } returntrue; } //插入排序(需排序数组,数组长度,排序次数) voidisInsertionSort(ElementTypeorigin[],intN,inttimes) { inti; ElementTypetemp=origin[times];//取出未排序序列中的第一个元素 for(i=times;i>0&&origin[i-1]>temp;i--) origin[i]=origin[i-1];//依次与已排序序列中元素比较并右移 origin[i]=temp; } voidSwap(ElementType*a,ElementType*b) { ElementTypet=*a; *a=*b; *b=t; } /*改编PercDown(MaxHeapH,intp)*/ voidPercDown(ElementTypeA[],intp,intN) { /*将N个元素的数组中以A[p]为根的子堆调整为最大堆*/ intParent,Child; ElementTypeX=A[p];/*取出根结点存放的值*/ for(Parent=p;(Parent*2+1)<N;Parent=Child){ Child=Parent*2+1; if((Child!=N-1)&&(A[Child]<A[Child+1])) Child++;/*Child指向左右子结点的较大者*/ if(X>=A[Child])break;/*找到了合适位置*/ else/*下滤X*/ A[Parent]=A[Child]; } A[Parent]=X; } voidHeapSort(ElementTypeA[],intN,intchanged[]) { for(inti=N/2-1;i>=0;i--)/*建立最大堆*/ PercDown(A,i,N); for(inti=N-1;i>0;i--){ /*删除最大堆顶*/ Swap(&A[0],&A[i]); PercDown(A,0,i); if(Judge(A,changed,N)){//是堆排序 printf("HeapSort\n"); Swap(&A[0],&A[i-1]);//在执行一次堆排序 PercDown(A,0,i-1); for(intj=0;j<N-1;j++) printf("%d",A[j]); printf("%d\n",A[N-1]); return; } } } intmain() { intN; intorigin[105],origin2[105],changed[105]; scanf("%d",&N); for(inti=0;i<N;i++){//originorigin1初始序列 scanf("%d",&origin[i]); origin2[i]=origin[i]; } for(inti=0;i<N;i++)//changed排序后序列 scanf("%d",&changed[i]); for(inti=1;i<N;i++){ isInsertionSort(origin,N,i); if(Judge(origin,changed,N)){//是插入排序 printf("InsertionSort\n"); isInsertionSort(origin,N,i+1); for(intj=0;j<N-1;j++) printf("%d",origin[j]); printf("%d\n",origin[N-1]); return0; } } HeapSort(origin2,N,changed); return0; }
相关文章推荐
- Fragment与Activity相互传递数据:
- TreeMap--左旋右旋
- jAVA EE里什么是链式调用
- mercurial(hg)使用
- C#中实现并发的几种方法的性能测试
- 浅谈 Swift 中的泛型
- 实例讲解Linux中cd命令切换目录的使用技巧
- centos 7 红帽认证 sshd服务搭建
- ajax+php数据增加查询获取删除
- HDU 1194 Beat the Spread! 【水题+细心】
- linux编程获取本机IP地址的三种方法
- vSphere 6.0 部署OVF模板失败,解决方法
- Java同步——可见性和原子性
- c++实验5—求和
- 设计模式之外观模式
- Extjs插入图片
- [No00008B]远程桌面发送“Ctrl+Alt+Delete”组合键调用任务管理器
- MFC对键盘消息的处理
- 关于cocos2d包上传google的openssh问题
- 用信号量解决读者写者问题