计算机算法导论-第8章.ppt_第1页
计算机算法导论-第8章.ppt_第2页
计算机算法导论-第8章.ppt_第3页
计算机算法导论-第8章.ppt_第4页
计算机算法导论-第8章.ppt_第5页
已阅读5页,还剩52页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论