September 26, 2005Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L5.1 Introduction to Algorithms 6.046J/18.401J Prof. Erik Demaine LECTURE5.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

Analysis of Algorithms
Heapsort O(n lg n) worst case Another design paradigm –Use of a data structure (heap) to manage information during execution of algorithm Comparision-based.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Non-Comparison Based Sorting
Linear Sorts Counting sort Bucket sort Radix sort.
CSE 3101: Introduction to the Design and Analysis of Algorithms
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Counting Sort Non-comparison sort. Precondition: n numbers in the range 1..k. Key ideas: For each x count the number C(x) of elements ≤ x Insert x at output.
CSCE 3110 Data Structures & Algorithm Analysis
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Lower bound for sorting, radix sort COMP171 Fall 2006.
1 SORTING Dan Barrish-Flood. 2 heapsort made file “3-Sorting-Intro-Heapsort.ppt”
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Comp 122, Spring 2004 Keys into Buckets: Lower bounds, Linear-time sort, & Hashing.
Comp 122, Spring 2004 Lower Bounds & Sorting in Linear Time.
Sorting. 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.
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
Lecture 5: Master Theorem and Linear Time Sorting
Analysis of Algorithms CS 477/677
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
September 24, 2001 L5.1 Introduction to Algorithms Lecture 6 Prof. Shafi Goldwasser.
David Luebke 1 8/17/2015 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Computer Algorithms Lecture 11 Sorting in Linear Time Ch. 8
Sorting in Linear Time Lower bound for comparison-based sorting
CSE 373 Data Structures Lecture 15
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Introduction to Algorithms Jiafen Liu Sept
Sorting Fun1 Chapter 4: Sorting     29  9.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
Analysis of Algorithms CS 477/677
Fall 2015 Lecture 4: Sorting in linear time
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
Linear Sorting. Comparison based sorting Any sorting algorithm which is based on comparing the input elements has a lower bound of Proof, since there.
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Lower Bounds & Sorting in Linear Time
Sorting.
Linear-Time Sorting Continued Medians and Order Statistics
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
CS200: Algorithm Analysis
Lecture 5 Algorithm Analysis
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
Ch8: Sorting in Linear Time Ming-Te Chi
Lecture 5 Algorithm Analysis
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
CS200: Algorithm Analysis
Linear Sorting Sorting in O(n) Jeff Chastine.
Linear Sort "Our intuition about the future is linear. But the reality of information technology is exponential, and that makes a profound difference.
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
Lecture 5 Algorithm Analysis
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
CS 583 Analysis of Algorithms
Lecture 5 Algorithm Analysis
Presentation transcript:

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.1 Introduction to Algorithms 6.046J/18.401J Prof. Erik Demaine LECTURE5 Sorting Lower Bounds Decision trees Linear-Time Sorting Counting sort Radix sort Appendix: Punched cards

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.2 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 best worst-case running time that we’ve seen for comparison sorting is O(nlgn). Is O(nlgn) the best we can do? Decision trees can help us answer this question.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.3 Decision-tree example Sort 〈 a 1, a 2, …, a n 〉 Each internal node is labeled i:j for i, j ∈ {1, 2,…, n}. The left subtree shows subsequent comparisons if a i ≤ a j. The right subtree shows subsequent comparisons if a i ≥ a j.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.4 Decision-tree example Each internal node is labeled i:j for i, j ∈ {1, 2,…, n}. The left subtree shows subsequent comparisons if a i ≤ a j. The right subtree shows subsequent comparisons if a i ≥ a j. Sort 〈 a 1, a 2, a 3 〉 = 〈 9, 4, 6 〉 :

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.5 Decision-tree example Each internal node is labeled i:j for i, j ∈ {1, 2,…, n}. The left subtree shows subsequent comparisons if a i ≤ a j. The right subtree shows subsequent comparisons if a i ≥ a j. Sort 〈 a 1, a 2, a 3 〉 = 〈 9, 4, 6 〉 :

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.6 Decision-tree example Each internal node is labeled i:j for i, j ∈ {1, 2,…, n}. The left subtree shows subsequent comparisons if a i ≤ a j. The right subtree shows subsequent comparisons if a i ≥ a j. Sort 〈 a 1, a 2, a 3 〉 = 〈 9, 4, 6 〉 :

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.7 Decision-tree example Sort 〈 a 1, a 2, a 3 〉 = 〈 9, 4, 6 〉 : Each leaf contains a permutation 〈 π(1), π(2),…, π(n) 〉 to indicate that the ordering a π(1) ≤ a π(2) ≤ …. ≤ a π(n) has been established.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.8 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 splitting 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.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.9 Lower bound for decision- tree sorting Theorem. Any decision tree that can sort n elements must have height Ω(nlgn). Proof. The tree must contain ≥n! leaves, since there are n! possible permutations. A height-h binary tree has ≤2 h leaves. Thus, n! ≤2 h. (lg is mono. Increasing) (Stirling’s formula)

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.10 Lower bound for comparison sorting Corollary. Heapsort and merge sort are asymptotically optimal comparison sorting algorithms.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.11 Sorting in linear time Counting sort: No comparisons between elements. Input: A[1.. n], where A[j] ∈ {1, 2, …, k}. Output: B[1.. n], sorted. Auxiliary storage: C[1.. k].

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.12 Counting sort for i←1 to k do C[i] ←0 for j←1 to n do C[A[j]] ← C[A[j]] + 1 ⊳ C[i] = |{key = i}| for i←2 to k do C[i] ←C[i] + C[i–1] ⊳ C[i] = |{key ≤i}| for j←n downto1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.13 Counting-sort example

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.14 Loop 1 for i←1 to k do C[i] ←0

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.15 Loop 2 for j←1 to n do C[A[j]] ←C[A[j]] + 1 ⊳ C[i] = |{key = i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.16 Loop 2 for j←1 to n do C[A[j]] ←C[A[j]] + 1 ⊳ C[i] = |{key = i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.17 Loop 2 for j←1 to n do C[A[j]] ←C[A[j]] + 1 ⊳ C[i] = |{key = i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.18 Loop 2 for j←1 to n do C[A[j]] ←C[A[j]] + 1 ⊳ C[i] = |{key = i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.19 Loop 2 for j←1 to n do C[A[j]] ←C[A[j]] + 1 ⊳ C[i] = |{key = i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.20 Loop 3 for i←2 to k do C[i] ←C[i] + C[i–1] ⊳ C[i] = |{key ≤i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.21 Loop 3 for i←2 to k do C[i] ←C[i] + C[i–1] ⊳ C[i] = |{key ≤i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.22 Loop 3 for i←2 to k do C[i] ←C[i] + C[i–1] ⊳ C[i] = |{key ≤i}|

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.23 Loop 4 for j←n downto 1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.24 Loop 4 for j←n downto 1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.25 Loop 4 for j←n downto 1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.26 Loop 4 for j←n downto 1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.27 Loop 4 for j←n downto 1 do B[C[A[j]]] ←A[j] C[A[j]] ←C[A[j]] –1

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.28 Analysis for to downto do

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.29 Running time If k = O(n), then counting sort takes (n) time. But, sorting takes Ω(nlgn) time! Where’s the fallacy? Answer: Comparison sorting takes Ω(nlgn) time. Counting sort is not a comparison sort. In fact, not a single comparison between elements occurs!

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.30 Stable sorting Counting sort is a stable sort: it preserves the input order among equal elements. Exercise: What other sorts have this property?

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.31 Radix sort Origin : Herman Hollerith’s card-sorting machine for the 1890 U.S. Census. (See Appendix.) Digit-by-digit sort. Hollerith’s original (bad) idea: sort on most-significant digit first. Good idea: Sort on least-significant digit first with auxiliary stable sort.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.32 Operation of radix sort

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.33 Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t –1digits. Sort on digit t

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.34 Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t –1digits. Sort on digit t   Two numbers that differ in digit t are correctly sorted.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.35 Correctness of radix sort Induction on digit position Assume that the numbers are sorted by their low-order t –1digits. Sort on digit t   Two numbers that differ in digit t are correctly sorted.   Two numbers equal in digit t are put in the same order as the input ⇒ correct order.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.36 Analysis of radix sort Assume counting sort is the auxiliary stable sort. Sort n computer words of b bits each. Each word can be viewed as having b/r base-2 r digits. Example: 32-bit word r = 8 ⇒ b/r = 4 passes of counting sort on base-2 8 digits; or r = 16 ⇒ b/r = 2 passes of counting sort on base-2 16 digits. How many passes should we make?

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.37 Analysis (continued) Recall: Counting sort takes (n + k) time to sort n numbers in the range from 0 to k –1. If each b-bit word is broken into r-bit pieces, each pass of counting sort takes (n + 2 r ) time. Since there are b/r passes, we have Choose r to minimize T(n,b): Increasing r means fewer passes, but as r ≫ lg n, the time grows exponentially.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.38 Choosing r Minimize T(n,b) by differentiating and setting to 0. Or, just observe that we don’t want 2 r ≫ n, and there’s no harm asymptotically in choosing r as large as possible subject to this constraint. Choosing r = lgn implies T(n,b) = (bn/lgn). For numbers in the range from 0 to n d –1, we have b =d lg n ⇒ radix sort runs in (dn) time.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.39 Conclusions In practice, radix sort is fast for large inputs, as well as simple to code and maintain. Example (32-bit numbers): At most 3 passes when sorting ≥2000 numbers. Merge sort and quick sort do at least lg2000 = 11passes. Downside: Unlike quicksort, radix sort displays little locality of reference, and thus a well-tuned quicksort fares better on modern processors, which feature steep memory hierarchies.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.40 Appendix: Punched-card technology Herman Hollerith ( ) Punched cards Hollerith’s tabulating system Operation of the sorter Origin of radix sort “Modern” IBM card Web resources on punched- card technology Return to last slide viewed.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.41 Herman Hollerith ( ) The 1880 U.S. Census took almost 10 years to process. While a lecturer at MIT, Hollerith prototyped punched-card technology. His machines, including a “card sorter,”allowed the 1890 census total to be reported in 6 weeks. He founded the Tabulating Machine Company in 1911, which merged with other companies in 1924 to form International Business Machines.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.42 Punched cards Punched card = data record. Hole = value. Algorithm = machine +human operator. Hollerith's tabulating system, punch card in Genealogy Article on the Internet Image removed due to copyright restrictions. Replica of punch card from the 1900 U.S. census. [Howells 2000]

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.43 Hollerith’s tabulating system Pantograph card punch Hand-press reader Dial counters Sorting box Image removed due to copyright restrictions. “Hollerith Tabulator and Sorter: Showing details of the mechanical counter and the tabulator press. ”Figure from [Howells 2000].

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.44 Operation of the sorter An operator inserts a card into the press. Pins on the press reach through the punched holes to make electrical contact with mercury- filled cups beneath the card. Whenever a particular digit value is punched, the lid of the corresponding sorting bin lifts. The operator deposits the card into the bin and closes the lid. When all cards have been processed, the front panel is opened, and the cards are collected in order, yielding one pass of a stable sort. Image removed due to copyright restrictions. Hollerith Tabulator, Pantograph, Press, and Sorter ( tabulator.html)

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.45 Origin of radix sort Hollerith’s original 1889 patent alludes to a most- significant-digit-first radix sort: “The most complicated combinations can readily be counted with comparatively few counters or relays by first assorting the cards according to the first items entering into the combinations, then reassorting each group according to the second item entering into the combination, and so on, and finally counting on a few counters the last item of the combination for each group of cards.” Least-significant-digit-first radix sort seems to be a folk invention originated by machine operators.

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.46 “Modern” IBM card One character per column. Image removed due to copyright restrictions. To view image, visit ges/ibmcard.jpg Produced by the WWW Virtual Punch- Card Server. So, that’s why text windows have 80 columns!

September 26, 2005Copyright © Erik D. Demaine and Charles E. Leiserson L5.47 Web resources on punched- card technology Doug Jones’s punched card index Biography of Herman Hollerith The 1890 U.S. Census Early history of IBM Pictures of Hollerith’s inventions Hollerith’s patent application (borrowed from Gordon Bell’s CyberMuseum) Impact of punched cards on U.S. history