版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AnIntroductiontoSortingChapterContentsOrganizingJavaMethodsthatSortanArraySelectionSortIterativeSelectionSortRecursiveSelectionSortTheEfficiencyofSelectionSortInsertionSortIterativeInsertionSortRecursiveInsertionSortTheEfficiencyofInsertionSortInsertionSortofaChainofLinkedNodesChapterContentsShellSortTheJavaCodeTheEfficiencyofShellSortComparingtheAlgorithmsOrganizingJavaMethods
thatSortanArrayConsideraclassofstaticsortmethodsThusabletocompareelementsinaninheritancehierarchy
OrganizingJavaMethods
thatSortanArrayFig.11-1TheclassGadgetisderivedfromtheclassWidget,whichimplementstheinterfaceComparableSelectionSortTask:rearrangebooksonshelfbyheightShortestbookontheleftApproach:Lookatbooks,selectshortestbookSwapwithfirstbookLookatremainingbooks,selectshortestSwapwithsecondbookRepeat…SelectionSortFig.11-2Beforeandafterexchangingshortestbookandthefirstbook.SelectionSortFig.11-3Aselectionsortofanarrayofintegersintoascendingorder.IterativeSelectionSortViewiterativealgorithmforselectionsortViewclassforsortinganarrayusingselectionsortSearchforindexofsmallestelementSwapvaluesinarrayelementsPutssmallestattopofcurrentsubarrayRecursiveSelectionSortViewrecursivealgorithmforselectionsortJavaimplementationofrecursiveselectionsortConsidergivingparametersforthearray,abeginningandendingindexforflexibilityTheEfficiencyofSelectionSortIterativemethodforloopexecutesn–1timesForeachofn–1calls,innerloopexecutes
n–2times(n–1)+(n–2)+…+1=n(n–1)/2=O(n2)RecursiveselectionsortperformssameoperationsAlsoO(n2)InsertionSortIffirsttwobooksareoutoforderRemovesecondbookSlidefirstbooktorightInsertremovedbookintofirstslotThenlookatthirdbook,ifitisoutoforderRemovethatbookSlide2ndbooktorightInsertremovedbookinto2ndslotRecheckfirsttwobooksagainEtc.InsertionSortFig.11-4Theplacementofthethirdbookduringaninsertionsort.InsertionSortFig.11-5AninsertionsortofbooksIterativeInsertionSortViewiterativealgorithmforinsertionsortNotethealgorithmfortheinsertoftheitemintothearrayFig.11-6AninsertionsortinsertsthenextunsortedelementintoitsproperlocationwithinthesortedportionofanarrayIterativeInsertionSortFig.11-7AninsertionsortofanarrayofintegersintoascendingorderRecursiveInsertionSortViewalgorithmforrecursiveinsertionsortJavaimplementationofrecursiveinsertionsortCanuseaniterativeinsertInOrder(seealgorithm)CanusearecursiveversionRecursiveInsertionSortFig.11-8Insertingthefirstunsortedelementintothesortedportionofthearray.(a)Theelementis≥lastsortedelement;(b)theelementis<thanlastsortedelementEfficiencyofInsertionSortBesttimeefficiencyisO(n)WorsttimeefficiencyisO(n2)IfarrayisclosertosortedorderLessworktheinsertionsortdoesMoreefficientthesortisInsertionsortisacceptableforsmallarraysizesInsertionSortofChainofLinkedNodesFig.11-9Achainofintegerssortedintoascendingorder.InsertionSortofChainofLinkedNodesFig.11-10Duringthetraversalofachaintolocatetheinsertionpoint,saveareferencetothenodebeforethecurrentone.InsertionSortofChainofLinkedNodesConsideraddingasortmethodtoclassLinkedChainListwhichusesADTlistMustimplementComparableViewcodeforsortingandinsertingInsertionSortofChainofLinkedNodesFig.11-11Breakingachainofnodesintotwopiecesasthefirststepinaninsertionsort:
(a)theoriginalchain;(b)thetwopiecesEfficiencyofinsertionsortofachainisO(n2)ShellSortAvariationoftheinsertionsortButfasterthanO(n2)DonebysortingsubarraysofequallyspacedindicesInsteadofmovingtoanadjacentlocationanelementmovesseverallocationsawayResultsinanalmostsortedarrayThisarraysortedefficientlywithordinaryinsertionsortShellSortFig.11-12Anarrayandthesubarraysformedbygroupingelementswhoseindicesare6apart.ShellSortFig.11-13ThesubarraysofFig.11-12aftertheyaresorted,andthearraythatcontainsthem.ShellSortFig.11-14ThesubarraysofthearrayinFig.11-13formedbygroupingelementswhoseindicesare3apartShellSortFig.11-15ThesubarraysofFig.11-14aftertheyaresorted,andthearraythatcontainsthem.Viewsourcecodefor
incrementalInsertionSortand
shellSortEfficiencyofShellSortEfficiencyisO(n2)forworstcaseIfnisapowerof2Average-casebehaviorisO(n1.5)Anytimethevariablespace(seesourcecode)iseven,add1ThisalsoresultsinO(n1.5)ComparingtheAlgorithms Best Average Worst
Case Case CaseSelectionsort O(n2) O(n2) O(n2)Insertionsort O(n) O(n2) O(n2)Shellsort O(n) O(n1.5) O(n1.5)Fig.11-16Thetimeefficienciesofthreesortingalgorithms,expressedinBigOhnotation.FasterSortingMethods2024年5月3日ChapterContentsMergeSortMergingArraysRecursiveMergeSortTheEfficiencyofMergeSortIterativeMergeSortMergeSortintheJavaClassLibraryChapterContentsQuickSortTheEfficiencyofQuickSortCreatingthePartitionJavaCodeforQuickSortQuickSortintheJavaClassLibraryRadixSortPseudocodeforRadixSortComparingtheAlgorithmsMergeSortDivideanarrayintohalvesSortthetwohalvesMergethemintoonesortedarrayReferredtoasadivideandconqueralgorithmThisisoftenpartofarecursivealgorithmHoweverrecursionisnotarequirementMergeSortFig.12-1Mergingtwosortedarraysintoonesortedarray.MergeSortFig.12-2Themajorstepsinamergesort.MergeSortViewalgorithmsmergeSortmergewhichisusedbymergeSortTracestepsofthealgorithminFigure12-3onnextslideMergeSortFig.12-3Theeffectoftherecursivecallsandthemergesduringamergesort.MergeSortEfficiencyMergesortisO(nlogn)inallcasesIt'sneedforatemporaryarrayisadisadvantageMergesortintheJavaClassLibraryTheclassArrayshassortroutinesthatusesthemergesortforarraysofobjectsMergeSortEfficiencyFig.12-4Aworst-casemergeoftwosortedarraysQuickSortDividesthearrayintotwopiecesNotnecessarilyhalvesofthearrayAnelementofthearrayisselectedasthepivotElementsarerearrangedsothat:ThepivotisinitsfinalpositioninsortedarrayElementsinpositionsbeforepivotarelessthanthepivotElementsafterthepivotaregreaterthanthepivotQuickSortViewQuickSortalgorithmFig.12-5Apartitionofanarrayduringaquicksort.QuickSortQuicksortisO(nlogn)intheaveragecaseO(n2)intheworstcaseWorstcasecanbeavoidedbycarefulchoiceofthepivotQuickSortFig.12-6Apartitionstrategyforquicksort…continued→QuickSortFig.12-6(ctd.)Apartitionstrategyforquicksort.QuickSortFig.12-7Median-of-threepivotselection:
(a)theoriginalarray;(b)thearraywithitsfirst,middle,andlastelementssortedQuickSortFig.12-8(a)Thearraywithitsfirst,middle,andlastelementssorted;(b)thearrayafterpositioningthepivotandjustbeforepartitioning.QuickSortQuicksortrearrangestheelementsinanarrayduringpartitioningprocessAftereachstepintheprocessOneelement(thepivot)isplacedinitscorrectsortedpositionTheelementsineachofthetwosubarraysRemainintheirrespectivesubarraysViewJavacodeforQuickSortNotesortmethodsinjava.utilforArraysclassRadixSortDoesnotcompareobjectsTreatsarrayelementsasiftheywerestringsofthesamelengthGroupselementsbyaspecifieddigitorcharacterofthestringElementsplacedinto"buckets"whichmatchthedigit(character)Originatedwithcardsorterswhencomputersused80columnpunchedcardsRadixSortFig.12-9(a)Originalarrayandbucketsafterfirstdistribution;(b)reorderedarrayandbucketsafterseconddistribution…continued→
RadixSortFig.12-9(c)reorderedarrayandbucketsafterthirddistribution;(d)sortedarrayRadixSortViewalgorithmforRadixSortNoteRadixsortisO(n)CanonlybeusedforcertainkindsofdataComparingtheAlgorithmsFig.12-10ThetimeefficiencyofvariousalgorithmsinBigOhnotationComparingtheAlgorithmsFig.12-11Acomparisonofgrowth-ratefunctionsasnincreases.AHeapImplementationChapterContentsReprise:TheADTHeapUsinganArraytoRepresentaHeapAddinganEntryRemovingtheRootCreatingaHeapHeapsortReprise:TheADTHeapAcompletebinarytreeNodescontainComparableobjectsInamaxheapObjectineachnode≥objectsindescendantsNotecontrastofusesoftheword"heap"TheADTheapTheheapoftheoperatingsystemfromwhichmemoryisallocatedwhennewexecutesReprise:TheADTHeapInterfaceusedforimplementationofmaxheapUsinganArraytoRepresentaHeapFig.28-1(a)Acompletebinarytreewithitsnodesnumberedinlevelorder;(b)itsrepresentationasanarray.UsinganArraytoRepresentaHeapWhenabinarytreeiscompleteCanuselevel-ordertraversaltostoredatainconsecutivelocationsofanarrayEnableseasylocationofthedatainanode'sparentorchildrenParentofanodeatiisfoundati/2
(unlessiis1)Childrenofnodeatifoundatindices
2iand2i+1ClicktoviewJavaimplementationofclassMaxHeapClassMaxHeapFig.28-2Thestepsinadding85tothemaxheapofFigure28-1aClassMaxHeapBeginatnextavailablepositionforaleafFollowpathfromthisleaftowardrootuntilfindcorrectpositionfornewentryAsthisisdoneMoveentriesfromparenttochildMakesroomfornewentryAddinganEntryFig.28-3ArevisionofstepsshowninFig.28-2toavoidswaps.….continued→AddinganEntryFig.28-3(ctd.)ArevisionofstepsshowninFig.28-2toavoidswaps.AddinganEntryFig.28-4AnarrayrepresentationofthestepsinFig.28-3…continued→AddinganEntryFig.28-4(ctd.)AnarrayrepresentationofthestepsinFig.28-3.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 恒生银行实习考察报告
- 内科医生的工作总结
- 化妆品中奎宁的检测方法
- 气象仪器产品商业计划书
- 书写工具架产品项目运营指导方案
- 专用分组交换机项目成效分析报告
- 雪茄用环状标签项目创业计划书
- 体重秤项目成效分析报告
- 半导体测试设备项目成效分析报告
- 家用电动意式面食制作机产品商业计划书
- 气道管理-气道湿化.ppt课件
- A4横线稿纸模板(可直接打印)-a4线条纸
- 幼儿园家长进课堂爱护眼睛(课堂PPT)
- 甲方如何做好对监理的管理
- (完整版)新版中日交流标准日本语初级下册课文译文及课后翻译
- 电梯应急救援演练方案
- JJG 195-2019连续累计自动衡器(皮带秤)检定规程(高清版)
- 一年级语文下册 第7单元 课文5 17 动物王国开大会作业设计 新人教版-新人教版小学一年级下册语文试题
- (完整版)C++必备专业英语单词(已标注音标)
- 2022年2022年最新小学数学课程标准
- 整形科护理工作制度
评论
0/150
提交评论