seperatethelistintotwopartionwiththepivotbetweenlow_positionandhigh_position.
recursivelydotheaboveoperation.
endif.
归并排序
Conceptually,amergesortworksasfollows:
Dividetheunsortedlistintonsublists,eachcontaining1element(alistof1elementisconsideredsorted).
Repeatedlymergesubliststoproducenewsortedsublistsuntilthereisonly1sublistremaining.Thiswillbethesortedlist.
归并排序的主要操作有:
1.将序列分成两部分;2.将两个部分进行排序整合;3.递归以上操作直至分成两个部分之后只剩一个元素。
顺序实现的归并排序算法在空间上会消耗较多。
1.2.5堆排序
首先,对序列进行堆初始化,因为堆排序仅适用于顺序表。
然后,向堆中添加元素时,先添加到堆的最末尾。
然后一路回溯至堆根部。
回溯过程中,判断是否满足堆的条件。
堆排序的时间复杂度为O(nlogn).
1.2.6树排序
1.2.7拓扑排序
查看文档:
7714519
1.前置条件
G是一个无有向环的有向图。
则G的拓扑次序是G中所有顶点的一个顺序排列
2.偏序和全序
3.常用的算法有Kahn算法和DFS算法
两者区别
Kahn算法不需要检测图为DAG,如果图为DAG,那么在出度为0的集合为空之后,图中还存在没有被移除的边,这就说明了图中存在环路。
而基于DFS的算法需要首先确定图为DAG,当然也能够做出适当调整,让环路的检测和拓扑排序同时进行,毕竟环路检测也能够在DFS的基础上进行。
二者的复杂度均为O(V+E)。
广度优先算法
一、无前趋的顶点优先的拓扑排序方法
该方法的每一步总是输出当前无前趋(即入度为零)的顶点,其抽象算法可描述为:
NonPreFirstTopSort(G){//优先输出无前趋的顶点while(G中有入度为0的顶点)do{从G中选择一个入度为0的顶点v且输出之;从G中删去v及其所有出边;}if(输出的顶点数目<|V(G)|)//若此条件不成立,则表示所有顶点均已输出
,排序成功。
Error("G中存在有向环,排序失败!
");}
深度优先算法
一、pseudocode
thatwillcontainthesortednodes
S←Setofallnodeswithnooutgoingedges
foreachnodeninSdo
visit(n)
functionvisit(noden)
ifnhasnotbeenvisitedyetthen
marknasvisited
foreachnodemwithanedgefrommtondo
visit(m)
addntoL
二、codeinC++
(见文档链接)
三、继的顶点优先拓扑排序方法1、思想方法该方法的每一步均是输出当前无后继(即出度为0)的顶点。
对于一个有向图,按此方法输出的序列是逆拓扑次序。
因此设置一个栈(或向量)T来保存输出的顶点序列,即可得到拓扑序列。
若T是栈,则每当输出顶点时,只需做入栈操作,排序完成时将栈中顶点依次出栈即可得拓扑序列。
若T是向量,则将输出的顶点从T[n-1]开始依次从后往前存放,即可保证T中存储的顶点是拓扑序列。
2、抽象算法描述算法的抽象描述为:
NonSuccFirstTopSort(G){//优先输出无后继的顶点while(G中有出度为0的顶点)do{从G中选一出度为0的顶点v且输出v;从G中删去v及v的所有人边}if(输出的顶点数目<|V(G)|)Error("G中存在有向环,排序失败!
");}
Kahn算法
一、eudocode
L←Emptylistthatwillcontainthesortedelements
S←Setofallnodeswithnoincomingedges
whileSisnon-emptydo
removeanodenfromS
addntotailofL
foreachnodemwithanedgeefromntomdo
removeedgeefromthegraph
ifmhasnootherincomingedgesthen
insertmintoS
ifgraphhasedgesthen
returnerror(graphhasatleastonecycle)
else
returnL(atopologicallysortedorder)
二、注意
1.需要记录的有:
顶点的连接关系、入度为0的顶点集合、存放排好序的顶点
2.需要的方法:
判断顶点入度是否为0、移走一条边、统计排好序的顶点
1.3表格信息检索
1.3.1基数排序
1.3.2哈希排序
查看文档:
13351072_郑泽丰_10__1
worst-casetimeforsearch,insert,anddeleteisO(size);ExpectedtimeisO
(1).
IdealHashing
•Usesa1Darray(ortable)table[0:
b-1].
–Eachpositionofthisarrayisabucket.
–Abucketcannormallyholdonlyonerecord.
•Usesahashfunctionfthatconvertseachkeykintoanindexintherange[0,b-1].
–f(k)isthehomebucketforkeyk.
•Everyrecord(key,element)isstoredinitshomebuckettable[f[key]].
•Acollisionoccurswhenthehomebucketforanewrecordisoccupiedbyarecordwithadifferentkey.
•Anoverflowoccurswhenthereisnospaceinthehomebucketforthenewpair.
•Whenabucketcanholdonlyonerecord,collisionsandoverflowsoccurtogether.
•Needmethodstohandlecollisions(andoverflows).
HashTableIssues
•Size(numberofbuckets)ofhashtable.
•Choiceofhashfunction.
•collisionshandlingmethod.
Twoprincipalcriteria:
ØItshouldbeeasyandquicktocompute.
–Convertkeyintoanintegerincasethekeyisnotaninteger.
•DonebythemethodhashCode().
ØItshouldachieveanevendistributionofthekeysthatactuallyoccuracrosstherangeofindices.
–f(k)isanintegerintherange[0,b-1],wherebisthenumberofbucketsinthetable.
choosingahashfunction
1.Truncation
●Thefirst,second,andfifthdigitsfromtherightmightmakethehashfunction,sothat21296876mapsto976.
2.Folding
●21296876mapsto212+968+76=1256.
3.ModularArithmetic
●Thebestchoiceformodulusisoften,butnotalways,aprimenumber.
CollisionResolutionwithOpenAddress
ØClustering
ØLinearProbing
ØIncrementFunctions
ØQuadraticProbing
ØKey-DependentIncrements
ØRandomProbing
1.4图的遍历
1.4.1广度优先搜索
1.Discription:
VisitstartvertexandputintoaFIFOqueue.
Repeatedlyremoveavertexfromthequeue,visititsunvisitedadjacentvertices,putnewlyvisitedverticesintothequeue.
2.Property
Allverticesreanchablefromthestartvertex(includingthestartvertex)arevisited.
1.4.2深度优先搜索
1.pseudocode:
depthFirstSearch(v)
{
Lablevertexvasreached.
for(eachunreachedvertexuadjacenctfromv)
depthFirstSearch(u);
}
2.prpgerties
samecomplexityasBFS;
1.5贪心算法
1.5.1最短路径
查看文档:
uid-27164517-id-3287891.html
Dijkstra算法
查看文档:
uid-20384806-id-1954190.html
一、前置条件
1.单源最短路径
2.有向图或者无向图
3.所有边权非负
二、算法描述
Letthenodeatwhichwearestartingbecalledtheinitialnode.LetthedistanceofnodeYbethedistancefromtheinitialnodetoY.Dijkstra'salgorithmwillassignsomeinitialdistancevaluesandwilltrytoimprovethemstepbystep.
1.Assigntoeverynodeatentativedistancevalue:
setittozeroforourinitialnodeandtoinfi