插入排序的改进Improved insertion sortWord格式.docx
- 文档编号:18236275
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:17
- 大小:20.48KB
插入排序的改进Improved insertion sortWord格式.docx
《插入排序的改进Improved insertion sortWord格式.docx》由会员分享,可在线阅读,更多相关《插入排序的改进Improved insertion sortWord格式.docx(17页珍藏版)》请在冰豆网上搜索。
<
(clock())-t/1000.0<
endl;
For(i=1;
a[i]<
'
;
endl<
(clock())-t/1000.0;
Justlookattheswap(a[j],a[j-1])ofasentenceandignorethecommentline,thiscodeislikebubblemethod,removethecommentline"
//"
moreeasilyunderstandtheforcycleJisinsertedintoa[i]froma[1]toa[i-1]orderedtheoriginalcolumn,butalsosignificantlyspeed.
2,withthelookouttofind,usetheloopexchangeforinsertion
Weknowthatswitchingtwosubscriptvariablesismoretime-consuming,andswitchingfromalargerangeofconsecutiveswitchestoaloopexchangewillreducetheamountofdatamovement.Toconsideryourselfasa"
surveillancepost"
youcanalsolimitthescopeofthecycleofjudgmentandthesizeofkeywordsasajudgment,whichsavessomejudgment.
{int,N,m,a[200001],I,J,K,t=clock();
For(j=1;
a[j]<
j++);
For(m=a[k=i];
k>
j;
k--)a[k]=a[k-1];
A[j]=m;
Withthesetwoimprovements,thespeedmaybedoubled.
3mergecycles,reducejudgments,andswaploops
Example:
NOIPimprovescombinationandfruit
Two,combinedfruit(fruit.pas/DPR/C/CPP)
[problemdescription]
Inanorchard,manyhavealreadyknockeddownallthefruit,andtheyaredividedintodifferentheapsaccordingtothedifferentkindsoffruit.Manydecidedtoheapallthefruittogether.
Eachtimethemerger,manycancombinetwopilesoffruittogether,andthestrengthconsumedisequaltothesumoftheweightofthetwopilesoffruit.Asyoucansee,allthefruithasbeenleftinthepileafterthen-1merger.Whencombinedwithfruit,thetotalamountofphysicalexertionisequaltothesumoftheenergyconsumedbyeachcombination.
Asmuchefforthasbeenmadetomovethefruithome,mucheffortisneededtoconserveenergywhencombinedwithfruit.Assumethateachfruitweightis1,andthenumberofspeciesknowntofruitandthenumberofeachtypeoffruit,yourtaskistodesignacombinedorderofprograms,soalotofphysicalleastcostandphysicaloutputoftheminimumcostvalue.
Forexample,thereare3kindsoffruit,thenumberis1,2,9.Youcancombinethe1and2heapsfirst,thenewheapnumberis3,andtheenergyconsumptionis3.Next,thenewheapwasmergedwiththeoriginalthird,andthenewheapwas12,consuming12ofthephysicaleffort.Somuchlaborcosts=3+12=15.Itcanbeprovedthat15istheminimumphysicalexertionvalue.
[inputfile]
Theinputfilefruit.incontainstworows,andthefirstlineisanintegern(1<
=n<
=10000)indicatingthenumberoffruits.Thesecondlinecontainsnintegers,separatedbyspaces.ThefirstIintegerAI(1<
=ai<
=20000)isthenumberofthefirstIspecies.
[outputfile]
Theoutputfilefruit.outcontainsarowthatcontainsonlyoneinteger,thatis,theminimumphysicalcost.Enterdatatomakesurethisvalueislessthan231.
[sampleinput]
Three
129
[sampleoutput]
Fifteen
[datasize]
For30%ofthedata,ensurethatthereisn<
=1000:
For50%ofthedata,ensurethatthereisn<
=5000;
Foralldata,besuretohaven<
=10000.
ThisproblemwithHuffmanalgorithm,eachtimetofindtwopilesofleastfruitformerger.Youcansortfirst,
Thisisequivalenttodeletingtwoofthefewestfruitsandcombiningthemintoanewpileoffruit.Andtheninsertthepileofnewfruitintoasequence.Repeatthisoperationuntilallfruitisinaheap.
Referencecode
#include<
iostream>
Inta[10001]={2147483647};
Voidinsert(int,x,int,y)
{
While(a[x]<
y)a[x+1]=a[x--];
A[x+1]=y;
Int,main()
Int,N,s=0;
Cin>
n;
For(inti=1;
Insert(i-1,a[i]);
For(inti=n-1;
i>
0;
i--)
S+=a[i]+=a[i+1];
Cout<
s;
System("
pause"
Return0;
4,twowayinsertion
Theabove3descriptionsaredescriptionsofthemagnitudeofthesquaredcalculations.Aswiththetwowayofsorting,theamountofcomputationisboundtodecreaseifwealsodotwo-wayprocessingforthesortoforderlookups.
Classexercise:
programmingcompletestwo-wayinsertion,ascendingsort.IinputtheamountofdataNandMboundaryvalue,thesecondrowisNtobesorteddata.Fordatathatissmallerthanthedemarcationvalue,theoriginalinsertionsort,insteadoflessthantheboundaryvalue,isinsertedbackwardfromthea[n]tothea[1].
Theboundaryvaluehere,ifyoudividetheNdataexactly,willgetthebesttwo-wayprocessingeffect,butthedistributionisnotenoughandtheaverageeffectwillbediscounted.Pleaseevaluatetheamountofcomputationthatdividesthe100dataintothefollowingthreepatterns:
5050
3070
1090
Canyoudescribethetwo-wayinsertionsortbythemostaverageallocation?
Let'
staketheCOUNTcompetitionasanexample,
NOIPimprovegroup2007
Firstquestion:
countstatistics
Enteranumbern(n<
=200000)andnnaturalnumbers(eachnumberisnotmorethan1.5*10^9),pleasecountthenumberofthesenaturalnumbersappear,insmallorderoutputfromsmalltolarge.Enterdatatoensurethatthenumberofdifferentdoesnotexceed10000.
Sampleinput:
Eight
Two
Four
Five
Onehundred
Sampleoutput:
23
42
51
1002
Thefollowingdescribesthetwowayinsertionusingabidirectionalchain
count.in"
Structnode
Int,Data,Count;
Node,*previous,*next;
};
Voidins(node,*q,int,x)
Node*p=newnode;
P->
Data=x;
Count=1;
previous=q;
Q->
next->
previous=p;
next=q->
next;
next=p;
Return;
Int,clockt=clock();
Node,l,m,R,*z,*t;
Z=&
m;
Int,x,N,I,b=0;
m.Data;
M.Count=1;
L.Data=-1;
R.Data=1500000001;
L.next=r.previous=z;
M.previous=&
l;
M.next=&
r;
x;
If(x==z->
Data)
Z->
Count++;
Elseif(x<
z->
{t=z;
Dot=t->
previous;
While(x<
t->
Data);
If(x==t->
T->
Else
Ins(T,X);
B--;
While(x>
Ins(t->
previous,X);
B++;
If(b<
-1)
Z=z->
B=0;
Elseif(b>
1)
T=l.next;
While(T,=&
r)
Data<
Count<
T=t->
(clock())-clockt/1000.0;
5.InsertasecondaryhashwithachaintoresolveCOUNT
IfthedatarangeinCOUNTischangedfromtheupperlimitof0<
x<
1.5*10^9to1.5*10^7,theselfmappedHashiisagoodchoice:
int[15000000];
main()
{intn,a,I,T=clock();
鳍>
n;
为(i=0;
i<
;
+(+))
对于(i=1;
15000000;
i+)
如果(b[我]>
0)四<
我<
”<
B[我]<
endl;
四<
(clock()-T)/1000;
面对大范围的数据,自映射哈希法的地址空间就不够了,压缩地址空间很可能造成地址冲突,采用链地址法就是解决地址冲突的常用方法之一。
#包括<
文件
#包含iostream>
<
使用名称空间;
ifstream鳍(“计算。
”);
ofstreamfout(“伯爵。
结点
int数据,计数;
下一个节点;
node()
下一个=0;
};
节点h[150001];
国际main()
int,i,j,临时;
节点p,*q;
对于(i=0;
150000;
i++)
h=新节点;
i=n;
散热片;
p=h[温度/10000];
同时(P>下一步)!
=0和温度>
p>
下一个>
数据
下一页;
如果(下)!
=0和p>
下一步>
数据=温度)
下面>
>
计数++;
其他的
q=新节点;
下一步;
p=下一个q;
q=数据=温度;
q=计数=1;
如果(下一步)!
=0)
而(P!
P->
数据<
P>
算<
系统(暂停);
返回0;
在4和5中,插入都避免了成片搬,把”插”的代价降低到了极点,但”查”的代价仍然很大。
为了降低”查”的代价,可以考虑折半查找。
6折半查找的插入排序
仍然以计数为例,下面的折半插入描述时间复杂度较低。
文件cstdlib>
ifstream鳍(“伯爵。
{int数据,计数;
节点A[10001];
int(int,int,int,x)
{整数;
而(R-L>
1)
{=(l+R)/2;
如果(x<
数据)
r=m;
L=m;
回我;
intmain(intargc、argvchar*[])
{[0]。
数据=1;
A[1]。
数据=2147483647;
整数n,i,x,t,d=1;
T=sizeof(节点);
滚动条;
X;
y=se(0,d,x);
如果(a=y)
{memcpy(+y+2,+y+1,T*(D-Y));
++;
一个[y+1];
a[y+1]。
计数=1;
i;
d;
一个[我]。
一个[我]数<
返回exit_success;
能否在线性结构”查”优化到log2(N)同时”插”也优化到O
(1)呢?
为什么?
7在直接交换的插入排序的基础上进行多路和多轮的改进--希尔排序
原来(52741386)
看成(5)
(2)(7)(4)
(1)(3)(8)(6)
第一轮子序列下标差4:
12745386
第二轮下标差2:
12537486
第三轮下标差1:
(12345678)
用它解决计数的参考代码
ifstreamCIN(“伯爵。
ofstreamcout(“伯爵。
int[200001];
n,m,i,j,t;
CIN;
m=n;
[i];
当((m=2)>
0)
对于(i=m;
a;
j,t,i+)
为(T=一个[J=我];
J>
=M&
&
T<[为了];
J=m)
a[j=m];
对于(m=i=1;
如果(一个[我]==一[·
])
米+;
cout<
一[·
]<
m=1;
ThetimecomplexityisO(n*log2(n)).Thefinalroundofthewhole
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 插入排序的改进Improved insertion sort 插入 排序 改进 Improved