Download presentation

Presentation is loading. Please wait.

Published byAugust Fields Modified over 2 years ago

1
Mudasser Naseer 1 5/1/2015 CSC 201: Design and Analysis of Algorithms Lecture # 9 Linear-Time Sorting Continued

2
Mudasser Naseer 2 5/1/2015 Review: Comparison Sorts ● Comparison sorts: O(n lg n) at best ■ Model sort with decision tree ■ Path down tree = execution trace of algorithm ■ Leaves of tree = possible permutations of input ■ Tree must have n! leaves, so O(n lg n) height

3
Mudasser Naseer 3 5/1/2015 Review: Counting Sort ● Counting sort: ■ Assumption: input is in the range 1..k ■ Basic idea: ○ Count number of elements k each element i ○ Use that number to place i in position k of sorted array ■ No comparisons! Runs in time O(n + k) ■ Stable sort ■ Does not sort in place: ○ O(n) array to hold sorted output ○ O(k) array for scratch storage

4
Mudasser Naseer 4 5/1/2015 Summary: Radix Sort ● Radix sort: ■ Assumption: input has d digits ranging from 0 to k ■ Basic idea: ○ Sort elements by digit starting with least significant ○ Use a stable sort (like counting sort) for each stage ■ Each pass over n numbers with d digits takes time O(n+k), so total time O(dn+dk) ○ When d is constant and k=O(n), takes O(n) time ■ Fast! Stable! Simple! ■ Doesn’t sort in place

5
Mudasser Naseer 5 5/1/2015 Bucket Sort ● Bucket sort ■ Assumption: input is n reals from [0, 1) ■ Basic idea: ○ Create n linked lists (buckets) to divide interval [0,1) into subintervals of size 1/n ○ Add each input element to appropriate bucket and sort buckets with insertion sort ■ Uniform input distribution O(1) bucket size ○ Therefore the expected total time is O(n) ■ These ideas will return when we study hash tables

6
Bucket Sort BUCKET-SORT(A) 1 n ← length[A] 2 for i ← 1 to n 3 do insert A[i ] into list B[ nA[i ] ] 4 for i ← 0 to n − 1 5 do sort list B[i ] with insertion sort 6 concatenate the lists B[0], B[1],..., B[n − 1] together in order Mudasser Naseer 6 5/1/2015

7
Mudasser Naseer 7 5/1/2015

8
● Correctness: Consider A[i ], A[ j ]. Assume without loss of generality that ● A[i ] ≤ A[ j ]. Then n · A[i ] ≤ n · A[ j ]. So A[i ] is placed into the same bucket as A[ j ] or into a bucket with a lower index. ■ If same bucket, insertion sort fixes up. ■ If earlier bucket, concatenation of lists fixes up. Mudasser Naseer 8 5/1/2015

9
Bucket Sort - Complexity ● All lines of algorithm except insertion sorting take (n) altogether. ● We “expect” each bucket to have few elements, since the average is 1 element per bucket. ● Intuitively, if each bucket gets a constant number of elements, it takes O(1) time to sort each bucket ⇒ O(n) sort time for all buckets. ● But we need to do a careful analysis. Mudasser Naseer 9 5/1/2015

10
Bucket Sort - Complexity Define a random variable: n i = the number of elements placed in bucket B[i ]. Because insertion sort runs in quadratic time, bucket sort time is T (n) = (n) + Take expectations of both sides E[T (n)] = Mudasser Naseer 10 5/1/2015

11
Bucket Sort - Complexity Where for i=0, 1, …., n-1 Mudasser Naseer 11 5/1/2015

12
Bucket Sort - Complexity Therefore: Mudasser Naseer 12 5/1/2015

13
Bucket Sort ● Again, not a comparison sort. Used a function of key values to index into an array. ● This is a probabilistic analysis - we used probability to analyze an algorithm whose running time depends on the distribution of inputs. ● With bucket sort, if the input isn’t drawn from a uniform distribution on [0, 1), all bets are off (performance-wise, but the algorithm is still correct). Mudasser Naseer 13 5/1/2015

Similar presentations

OK

Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)

Lower Bounds for Comparison-Based Sorting Algorithms (Ch. 8)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on earthquake for class 10 Ppt on atrial septal defect secundum Download ppt on pulse code modulation video Ppt on customer based brand equity Free ppt on brain machine interface articles Ppt on standing order forms Ppt on edge detection python World map download ppt on pollution Ppt on children's day Ppt on world population