版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 社区志愿者活动心得体会
- 《生物入侵者》教学设计【共2篇】
- 课堂观察报告范文
- 二手房交易合同范本
- 五年级《组合图形的面积》教学设计
- 展厅装修合同
- 消防技术服务(检测)合同
- 河南省焦作市2023-2024学年八年级上学期期末英语试题(含听力)(含答案解析)
- 电感耦合等离子体光谱仪操作指导书
- 应急预案内部评审记录
- 食材配送应急预案(完整版)
- 劳务派遣投标服务方案
- 七年级下学期家长会课件优质
- 新版压力容器制造质量手册
- 职业生涯规划各章复习要点
- UnityPro编程培训教程第05章变量及数据
- JJF 1903-2021 冲击响应谱试验机校准规范
- 龙门式机械手结构设计
- 临时用水施工方案技术交底
- 美国社会与文化智慧树知到答案章节测试2023年天津外国语大学
评论
0/150
提交评论