Lower bound for sorting, radix sort

COMP171 Fa2006 Lower bound for sorting radix sort
Lower bound for sorting, radix sort COMP171 Fall 2006

Sorting N/Slide 2 Lower Bound for Sorting Mergesort and heapsort worst-case running time is o(N log N) Are there better algorithms? Goal: Prove that any sorting algorithm based on only comparisons takes 2(N log n) comparisons in the worst case(worse-case input) to sort n elements
Sorting IV / Slide 2 Lower Bound for Sorting Mergesort and heapsort worst-case running time is O(N log N) Are there better algorithms? Goal: Prove that any sorting algorithm based on only comparisons takes (N log N) comparisons in the worst case (worse-case input) to sort N elements

Sorting N/Slide 3 Lower bound for sorting Suppose we want to sort n distinct elements How many possible orderings do we have for Elements? We can have N! possible orderings(e.g, the sorted output for a, b, c can be a b c, b a c, acb, c ab, cb a, bca)
Sorting IV / Slide 3 Lower Bound for Sorting Suppose we want to sort N distinct elements How many possible orderings do we have for N elements? We can have N! possible orderings (e.g., the sorted output for a,b,c can be a b c, b a c, a c b, c a b, c b a, b c a.)

Sorting N/Slide 4 Lower bound for sorting Any comparison-based sorting process can be represented as a binary decision tree Each node represents a set of possible orderings consistent with all the comparisons that have been made The tree edges are results of the comparisons
Sorting IV / Slide 4 Lower Bound for Sorting Any comparison-based sorting process can be represented as a binary decision tree. Each node represents a set of possible orderings, consistent with all the comparisons that have been made The tree edges are results of the comparisons

Sorting N/Slide 5 <b< a<c<b Decision tree for b<a<c Algorithm X for sorting b<c<a c<asb three elements a, b, c c<b<a b<a < b a<c<b c<b<a c<a<b b<c c<b a<c c<a b<a<c c<b<a b<c c<asb b<c<a a <c<b a<c c<a <c < b b<a<c a <b<
Sorting IV / Slide 5 Decision tree for Algorithm X for sorting three elements a, b, c

Sorting N/Slide 6 Lower Bound for Sorting a different algorithm would have a different decision tree Decision tree for insertion sort on 3 elements al: a2 a2:a3 al: a3 (al,a2,a3) al: a3 (a2, a1, a3) a2: a (al,a3,a2) (a3,al,a2) (a2, a3, al (a3,a2,al) There exists an input ordering that corresponds to each root-to-leaf path to arrive at a sorted order. For decision tree of insertion sort, the longest path is O(N2)
Sorting IV / Slide 6 Lower Bound for Sorting A different algorithm would have a different decision tree Decision tree for Insertion Sort on 3 elements: There exists an input ordering that corresponds to each root-to-leaf path to arrive at a sorted order. For decision tree of insertion sort, the longest path is O(N2 )

Sorting N/Slide 7 Lower bound for sorting The worst-case number of comparisons used by the sorting algorithm is equal to the depth of the deepest leaf The average number of comparisons used is equal to the average depth of the leaves a decision tree to sort n elements must have N! eaves a binary tree of depth d has at most 2d leaves > a binary tree with 2d leaves must have depth at least d the decision tree with N! leaves must have depth at least log2(N! Therefore, any sorting algorithm based on only comparisons between elements requires at least I log2 (N )I comparisons in the worst case
Sorting IV / Slide 7 Lower Bound for Sorting The worst-case number of comparisons used by the sorting algorithm is equal to the depth of the deepest leaf The average number of comparisons used is equal to the average depth of the leaves A decision tree to sort N elements must have N! leaves a binary tree of depth d has at most 2 d leaves a binary tree with 2d leaves must have depth at least d the decision tree with N! leaves must have depth at least log2 (N!) Therefore, any sorting algorithm based on only comparisons between elements requires at least log2 (N!) comparisons in the worst case

Sorting N/Slide 8 Lower Bound for Sorting log2(N])=log(N(N-1)(N-2)…(2)(1) logN+log(N-1)+log(N一2)+…+log2+log1 log N +log(N-1)+log(N-2)+ .+log(N/2) N log w g &(Nlog N) Any sorting algorithm based on comparisons between elements requires Q2(N log N comparisons
Sorting IV / Slide 8 Lower Bound for Sorting Any sorting algorithm based on comparisons between elements requires (N log N) comparisons

Sorting N/Slide 9 Linear time sorting Can we do better(inear time algorithm)if the input has special structure(e.g, uniformly distributed, every number can be represented by d digits)? Yes Counting sort, radix sort
Sorting IV / Slide 9 Linear time sorting Can we do better (linear time algorithm) if the input has special structure (e.g., uniformly distributed, every number can be represented by d digits)? Yes. Counting sort, radix sort

Sorting N /Slide 10 Counting sort Assume n integers are to be sorted, each is in the range 1 to M Define an array b[1.M], initialize all toO O(M) Scan through the input list A[, insert A[] into B[AQI= O(N) Scan B once, read out the nonzero integers= O(M) otal time: O(M+ N) if M is o(n), then total time is O(N) Can be bad if range is very big, e.g. M=O(N2) N=7,M=9, Want to sort 8 19 526 3 11235689 Output: 123568 9
Sorting IV / Slide 10 Counting Sort Assume N integers are to be sorted, each is in the range 1 to M. Define an array B[1..M], initialize all to 0 O(M) Scan through the input list A[i], insert A[i] into B[A[i]] O(N) Scan B once, read out the nonzero integers O(M) Total time: O(M + N) if M is O(N), then total time is O(N) Can be bad if range is very big, e.g. M=O(N2 ) N=7, M = 9, Want to sort 8 1 9 5 2 6 3 1 2 5 8 9 Output: 1 2 3 5 6 8 9 3 6
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 数据传送类指令(PPT讲稿).ppt
- 长春工业大学:《电子商务》课程教学资源(PPT课件)第9章 网络鞋城前台页面.ppt
- 因特网多媒体技术(PPT讲稿).ppt
- International Trade Forms.ppt
- 香港理工大学:Building Robust Wireless LAN for Industrial Control with DSSS-CDMA Cell Phone Network Paradigm.ppt
- 香港浸会大学:《Experiencing Cluster Computing》Class 8 Case Studies.ppt
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)动态调度(Cont)、推断执行和ILP.ppt
- 《多媒体教学软件设计》课程PPT教学课件:第13章 多媒体教学软件中脚本编程技巧.ppt
- 山西国际商务职业学院:《网页设计与制作》课程教学资源(PPT课件)第一章 网页设计基础知识.ppt
- 《算法设计技巧与分析》课程教学资源(PPT讲稿)Lecture 8 贪婪法则 Greedy Approach.ppt
- 山东大学:《计算机图形学》课程PPT教学课件(Programming with OpenGL)Part 3:Three Dimensions.ppt
- Integrated analysis of regulatoryand metabolic networks revealsnovel regulatory mechanisms inSaccharomyces cerevisiae.ppt
- 基于语义关联和信息增益的TFIDF改进算法研究.ppt
- 《C程序设计》课程PPT教学课件(电子教案)第六章 函数.ppt
- 安徽理工大学:《汇编语言》课程教学资源(PPT课件讲稿)第五章 循环与分支程序设计.ppt
- 四川大学:Object-Oriented Design and Programming(Java,PPT课件).ppt
- 《编译原理和技术》课程PPT教学课件:第十三章 函数式语言的编译.ppt
- 《Microsoft Access 2003》教程PPT:第9章 报表设计.ppt
- 北京大学远程教育:《计算机应用基础》课程PPT教学课件(专科)串讲(综合复习).pptx
- 计算机问题求解(PPT讲稿)B树.pptx
- 《ASP动态网页设计实用教程》教学资源(PPT课件讲稿)第8章 Web数据库基础.ppt
- 卷积码的概率译码(PPT讲稿).ppt
- 电子工业出版社:《计算机网络》课程教学资源(第五版,PPT课件讲稿)第十章 下一代因特网.ppt
- 复旦大学:Trapping in scale-free networks with hierarchical organization of modularity.pptx
- Network and System Security Risk Assessment(PPT讲稿)Introduction.ppt
- 香港科技大学:Latent Tree Models.pptx
- 《汇编语言程序设计》课程教学资源(PPT课件讲稿)循环与分支程序设计.ppt
- ARM Tachnology:Chapter 3 STM32 Clock and Configuration.ppt
- 《软件工程简介》课程PPT教学课件(可行性研究、需求分析、总体设计、详细设计).ppt
- 利用NetRiver实验系统实现IP协议交互和TCP协议交互.ppt
- 江苏海洋大学(淮海工学院):《Java面向对象程序设计》课程教学资源(PPT课件讲稿)第3章 Java 面向对象编程 3.1 面向对象软件开发概述.pptx
- 《数字图像处理 Digital Image Processing》课程教学资源(PPT课件讲稿)第2章 图像的基本知识及运算.ppt
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 02 进程和线程 Processes and Threads.ppt
- 《计算机辅助设计 Computer Aided Design》课程PPT教学课件:第一篇 CAD技术 第一章 几何造型方法介绍和分类.ppt
- 清华大学:高校信息门户建设(PPT讲稿).ppt
- 《汇编语言》课程PPT教学课件:第三章 80x86寻址方式和指令系统.ppt
- 《网站设计与建设 Website design and developments》课程教学资源(PPT课件讲稿)第一部分 Web基础知识 第3章 图形与Web设计.ppt
- 香港城市大学:Introduction to Real-Time Systems(Design and Analysis of Algorithms).pptx
- 《编译原理》课程教学资源(PPT课件讲稿)第五章 语法分析——自下而上分析.ppt
- 香港科技大学:Advanced Topics in NextGeneration Wireless Networks.ppt