Ch8: Sorting in Linear Time Ming-Te Chi

Slides:



Advertisements
Similar presentations
Analysis of Algorithms
Advertisements

Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Non-Comparison Based Sorting
MS 101: Algorithms Instructor Neelima Gupta
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lower bound for sorting, radix sort COMP171 Fall 2005.
Using Divide and Conquer for Sorting
Lower bound for sorting, radix sort COMP171 Fall 2006.
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.
Lecture 5: Master Theorem and Linear Time Sorting
8.Sorting in linear time Hsu, Lih-Hsing. Computer Theory Lab. Chapter 8P Lower bound for sorting The decision tree model.
Analysis of Algorithms CS 477/677
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
David Luebke 1 7/2/2015 Linear-Time Sorting Algorithms.
Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)
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?
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
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,
CS6045: Advanced Algorithms Sorting Algorithms. Heap Data Structure A heap (nearly complete binary tree) can be stored as an array A –Root of tree is.
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.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
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.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Advanced Sorting 7 2  9 4   2   4   7
Lower Bounds & Sorting in Linear Time
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Sorting.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
David Kauchak cs062 Spring 2010
Linear-Time Sorting Continued Medians and Order Statistics
MCA 301: Design and Analysis of Algorithms
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Quick-Sort 11/14/2018 2:17 PM Chapter 4: Sorting    7 9
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
Quick-Sort 11/19/ :46 AM Chapter 4: Sorting    7 9
Keys into Buckets: Lower bounds, Linear-time sort, & Hashing
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.
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Linear Sorting Sorting in O(n) Jeff Chastine.
Lower Bounds & Sorting in Linear Time
Linear-Time Sorting Algorithms
Quick-Sort 2/23/2019 1:48 AM Chapter 4: Sorting    7 9
Lecture 5 Algorithm Analysis
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Lower bound for sorting, radix sort
Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.
CS 583 Analysis of Algorithms
The Selection Problem.
Linear Time Sorting.
Sorting We have actually seen already two efficient ways to sort:
Lecture 5 Algorithm Analysis
Presentation transcript:

Ch8: Sorting in Linear Time Ming-Te Chi Algorithms Ch8: Sorting in Linear Time Ming-Te Chi Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Overview This week topics:sorting Next topics:searching and related subjects. Computer is powerful in manipulating data Store data and perform operations on data Search/retrieve Insert Delete Other operations Ch8 Sorting in Linear Time

Sorting – how fast can we sort? Selection or insertion sort Merge sort Heap sort Quick sort . . . Complexity (time) is either O(n log n) or O(n2) Ch8 Sorting in Linear Time

Lower bounds for sorting to examine all the input. All sorts seen so far are We’ll show that is a lower bound for comparison sorts. Ch8 Sorting in Linear Time

Sorting based on Comparison Selection sort, insertion sort, merge sort, heap sort, quick sort All have time complexity Based on comparison between the input elements Comparison sorts can be viewed abstractly in terms of decision tree Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Decision tree Fully binary tree Abstraction of any comparison sort. Represents comparisons made by a specific sorting algorithm on inputs of a given size Abstracts away everything else: control and data movement. We’re counting only comparisons Ch8 Sorting in Linear Time

Example: Decision Tree for Sorting Three Element {a, b, c} a:b a ≤ b a > b a:c b:c b ≤ c a ≤ c a > c b > c b:c cab a:c cba b ≤ c a ≤ c b > c a > c abc acb bac bca Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Each leaf is labeled by the permutation of orders that the algorithm determines Any correct sorting algorithm must be able to produce each permutation of its input. A correct comparison sort algorithm requires that each of the permutations must appear as one of the leaves of the decision tree. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Each of these leaves must be reachable from the root by a path corresponding to an actual execution of the comparison sort. The execution of the sorting algorithm corresponds to tracing a path from the root of the decision tree to a leaf. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time How many leaves? What is the height of the tree? Ch8 Sorting in Linear Time

Properties of the decision tree At least n! leaves For any comparison sort 1 tree for each n View the tree as if the algorithm splits in two at each node, based on the information it has determined up to that point. The tree models all possible execution traces. Ch8 Sorting in Linear Time

Properties of the decision tree (continue) Length of the longest path from root to leaf? The worst-case number of comparisons that the corresponding sorting algorithm performs. Depends on the algorithm Insertion sort: Merge sort: Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Bucket Sort: linear time sorting Allocate sufficient number of buckets & put element in corresponding buckets at the end, scan the buckets in order & collect all elements n elements, ranges from 1 to m  m buckets O(m+n) Best case: O(n), Worst case: O(m) Ch8 Sorting in Linear Time

Sorting in linear time: 8.2 Counting sort (similar to bucket sort) Depends on a key assumption: n numbers to be sorted are integers in {0, 1, . . . , k}. Input: A[1 . . n], where A[ j ] ∈ {0, 1, . . . , k} for j = 1, 2, . . . , n. (Array A and values n and k are given as parameters.) Output: B[1 . . n], sorted. (B is assumed to be already allocated and is given as a parameter.) Auxiliary storage: C[0 . . k] Ch8 Sorting in Linear Time

Sorting in linear time: 8.2 Counting sort Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time How big a k is practical? Good for sorting 32-bit values? No. 16-bit? Probably not. 8-bit? Maybe, depending on n. 4-bit? Probably (unless n is really small) Example: playing cards. (撲克牌) Counting sort (bucket sort) will be used in radix sort. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time 8.3 Radix sort Used by the card-sorting machines one can now find only in computer museum. Sort the least significant digit first RADIX_SORT(A,d) 1 for i  1 to d 2 do use a stable sort to sort array A on digit i Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Analysis: Radix sort Assume that we use counting sort as the intermediate sort. per pass (digits in range 0, . . . , k) d passes total If , time complexity is Not in-place (in-place: only constant number of elements of the input array are ever stored outside the array. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Sample question: What is the decision tree? Explain how it can be used to prove the lower bound of the comparison based sorting algorithms. It is well known that radix sort has the time complexity of O(d n), where d is the maximal number of digits of all the inputs and n is the total number of inputs. Are these two results contradict to each other? Justify your answer. Ch8 Sorting in Linear Time

Bucket sort (new version) Divide the interval [0, 1) into n equal-sized subintervals (buckets). Distribute the n input numbers into the buckets. Input: n-element array A, with Needs an auxiliary array B[0..n-1] of linked lists. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Analysis The running time of bucket sort is where ni is the number of elements placed in the bucket B[i]. Why? Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Analysis (continue) Taking expectations of both sides and using linearity of expectation, we have Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time We claim that We define indicator random variables Xij = I {A[j] falls in bucket i} for i = 0, 1, …, n-1 and j = 1, 2,…,n. thus, Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Indicator random variable Xij is 1 with probability 1/n and 0 otherwise, and therefore When k  j, the variables Xij and Xik are independent, and hence Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time We can conclude that the expected time for bucket sort is (n)+n·O(2-1/n)= (n). Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time

Ch8 Sorting in Linear Time Remarks: Decision tree proves that O(n log n) is required for sorting Radix sort (or bucket sort) states that O(n) is enough for sorting Is there anything wrong? Ch8 Sorting in Linear Time