




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 7: Sorting AlgorithmsQuick SortMark Allen Weiss: Data Structures and Algorithm Analysis in JavaLydia Sinapova, Simpson College1Quick SortBasic IdeaCodeAnalysisAdvantages and DisadvantagesApplicationsComparison with Heap sort and Merge sortAnimation2Basic IdeaPick one element in the array, wh
2、ich will be the pivot. Make one pass through the array, called a partition step, re-arranging the entries so that: entries smaller than the pivot are to the left of the pivot. entries larger than the pivot are to the right 3Basic IdeaRecursively apply quicksort to the part of the array that is to th
3、e left of the pivot, and to the part on its right.No merge step, at the end all the elements are in the proper order4Choosing the PivotSome fixed element: e.g. the first, the last, the one in the middle.Bad choice - may turn to be the smallest or the largest element, then one of the partitions will
4、be emptyRandomly chosen (by random generator) - still a bad choice5Choosing the PivotThe median of the array (if the array has N numbers, the median is the N/2 largest number). This is difficult to compute - increases the complexity. 6Choosing the PivotThe median-of-three choice: take the first, the
5、 last and the middle element. Choose the median of these three elements. 7Find the Pivot Java Codeint median3 ( int a, int left, int right) int center = (left + right)/2; if (a left a center) swap (a left , a center); if (a center a right) swap (acenter, a right ); if (a left a center) swap (a left
6、, a center); swap(a center , a right-1 ); return a right-1 ;8Quick Sort Java CodeIf ( left + 10 = right) / do quick sort else insertionSort (a, left, right);9Quick Sort Java Code int i = left, j = right - 1; for ( ; ; ) while (a + i pivot) while ( pivot a - - j ) if (i j) swap (a i , a j ); else bre
7、ak; swap (a i , a right-1 ); quickSort ( a, left, i-1); quickSort (a, i+1, right); 10Implementation NotesCompare the two versions:A.while (a+i pivot) while (pivot a-j) if ( i j ) swap (ai, aj);else break;B. while (a i pivot) i+ ; while (pivot a j ) j- - ; if ( i 1 Telescoping:T(N-1) = T(N-2) + c(N-1
8、)T(N-2) = T(N-3) + c(N-2)T(N-3) = T(N-4) + c(N-3) .T(2) = T(1) + c.2 16Worst-Case AnalysisT(N) + T(N-1) + T(N-2) + + T(2) = T(N-1) + T(N-2) + + T(2) + T(1) + c(N) + c(N-1) + c(N-2) + + c.2T(N) = T(1) + c times (the sum of 2 thru N) = T(1) + c (N (N+1) / 2 -1) = O(N2)17Best-Case AnalysisThe pivot is
9、in the middleT(N) = 2T(N/2) + cNDivide by N:T(N) / N = T(N/2) / (N/2) + c18Best-Case AnalysisTelescoping:T(N) / N = T(N/2) / (N/2) + cT(N/2) / (N/2) = T(N/4) / (N/4) + cT(N/4) / (N/4) = T(N/8) / (N/8) + cT(2) / 2 = T(1) / (1) + c19Best-Case AnalysisAdd all equations:T(N) / N + T(N/2) / (N/2) + T(N/4
10、) / (N/4) + . + T(2) / 2 = = (N/2) / (N/2) + T(N/4) / (N/4) + + T(1) / (1) + c.logNAfter crossing the equal terms: T(N)/N = T(1) + c*LogNT(N) = N + N*c*LogN = O(NlogN)20Advantages and DisadvantagesAdvantages:One of the fastest algorithms on averageDoes not need additional memory (the sorting takes p
11、lace in the array - this is called in-place processing )Disadvantages:The worst-case complexity is O(N2)21ApplicationsCommercial applications QuickSort generally runs fast No additional memoryThe above advantages compensate for the rare occasions when it runs with O(N2)22ApplicationsNever use in app
12、lications which require a guarantee of response time:Life-critical (medical monitoring, life support in aircraft, space craft) Mission-critical (industrial monitoring and control in handling dangerous materials, control for aircraft, defense, etc) unless you assume the worst-case response timeWarning:23Comparison with Heap Sort O(NlogN) complexity quick sort runs faster, (does not support a heap tree)the speed of quick sort is not guaranteed 24Compa
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 实习生签约协议书
- 岗位劳动合同汇编二零二五年
- 二零二五版租赁院落合同
- 住宅铺位合同标准文本
- 房屋买卖协议合同书模板二零二五年
- 二零二五全新一部分股份转让协议
- 买卖楼房佣金合同样本
- 中标后实验设备合同样本
- 供热管网维护合同样本
- 个人采购材料合同样本
- 2025年上半年上海青浦新城发展(集团)限公司自主招聘9名易考易错模拟试题(共500题)试卷后附参考答案
- 墙纸墙布施工工艺标准化流程
- 水泥混凝土路面翻修施工方案详解
- 《射雕英雄传》好书读后感
- DB51T 2049-2015 建筑消防设施检测规范
- 【MOOC】风景背后的地貌学-华中师范大学 中国大学慕课MOOC答案
- 护理感动案例
- 2024版《安全生产法》考试题库附答案(共90题)
- 企业天然气转让协议书范文范本
- 带式运输机传动装置的设计
- 玩具照相机细分市场深度研究报告
评论
0/150
提交评论