版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Introduction to Algorithms 计算机算法导论,20072008年第一学期,Sorting and Order Statistics,Introduction,Sorting problem,Definition: Input: A sequence of numbers. Output: A permutation , such that a1 a2 an .,The structure of the data,Definition: Record = key + satellite data Assumption: The input consists only of
2、 numbers,Why sorting?,The need inherent in an application Algorithms often use sorting as a key subroutine A wide variety of sorting algorithms, a rich set of techniques A problem can be proved a nontrivial lower bound. Many engineering issues come to fore when implementing sorting algorithms.,Sorti
3、ng algorithms,A in-place sorting algorithm Comparison sort The counting sort algorithm The radix sort algorithm The bucket sort algorithm,Order statistics,The ith order statistic of a set of n numbers is the ith smallest number in the set.,8、 Sorting in linear time,8.1 Lower bounds for sorting,Assum
4、ption: All of the input elements are distinct All comparisons have the form ai aj,How fast can we sort?,All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order of elements. E.g.,insertion sort, merge sort, quicksort, heapsort. The bes
5、t worst-case running time that weve seen for comparison sorting is O(n lg n) . Is O(n lg n) the best we can do? Decision trees can help us answer this question.,Decision-tree model,A decision tree can model the execution of any comparison sort: One tree for each input size n. View the algorithm as s
6、plitting whenever it compares two elements. The tree contains the comparisons along all possible instruction traces. The running time of the algorithm = the length of the path taken. Worst-case running time = height of tree.,Lower bound for decision-tree sorting,Lower bound for comparison sorting,Co
7、rollary. Heapsort and merge sort are asymptotically optimal comparison sorting algorithms.,8.2 Counting sort,Sorting in linear time Counting sort: No comparisons between elements. Input: A1 . . n, where A j1, 2, , k . Output: B1 . . n, sorted. Auxiliary storage: C1 . . k .,Counting sort,for i 1 to k
8、 do Ci 0 for j 1 to n do CA j CA j + 1 Ci = |key = i| for i 2 to k do Ci Ci + Ci1 Ci = |key i| for j n downto 1 do BCA j A j CA j CA j 1,Running time,If k = O(n), then counting sort takes (n) time. But, sorting takes (n lg n) time! Wheres the fallacy? Answer: Comparison sorting takes (n lg n) time.
9、Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs!,8.3 Radix sort, Origin: Herman Holleriths card-sorting machine for the 1890 U.S. Census. (See Appendix .) Digit-by-digit sort. Holleriths original (bad) idea: sort on most-significant digit first. Good idea: Sort on least-significant digit first with auxiliary stable sort.,8.4 Bucket so
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年护理主管护师考试题及答案
- PTCD导管围术期护理实操教学
- 2026年建筑项目负责人(安全员B证)考试题库(附答案)
- 2026年节水知识竞赛试题库
- 2026年老年护理能力考核题及答案
- 2026年美发师四级练习题库及参考答案
- 2026年平衡膳食知识试题及答案考前必看
- 2026年全套蓝色的橙汁测试题及答案
- 2026年人工智能训练师(三级)职业技能鉴定理论考试题库(含答案)
- 企业管理-村里消防管理制度
- 吉利汽车GEELY+品牌VI手册 Geely Auto Communication Guidelines (New Energy 2025)
- 电缆绝缘检测方法
- 2026年山东名校考试联盟5月联考(核心素养评估)地理试题(含答案)
- 离子束抛光控制算法:原理、应用与优化策略
- 化工园区多米诺效应分析
- 新课标引领下高中地理课堂教学设计的创新转型
- 35KV变电站施工方案
- 跳蚤的自我设障课件
- 2024年山东大学校长开学讲话稿8000字
- 学习《水利水电工程生产安全重大事故隐患判定导则-SLT 842》课件
- (2025)医院招聘护士考试题库(附参考答案)
评论
0/150
提交评论