您的位置:首页 > 其它

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?

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;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: