Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to.

Similar presentations


Presentation on theme: "Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to."— Presentation transcript:

1 Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to sort n elements has a tight lower bound of nlg(n). To beat nlg(n) requires information about the input in addition to the values of the elements to be sorted Counting and Bucket sort are examples where we have this additional information that permits linear sorting No HW assignments. Results used in future HW assignments

2 Lower bound on comparison sorts:
Assume elements are distinct Finite number of non-distinct elements does not affect asymptotic behavior. For distinct elements, all the information that can be gained from comparison of elements is contained in the question Is “aj > ak?” This reduces comparison sorts to binary decision trees.

3 Decision tree to sort 3 distinct elements
Each node (labeled j:k) denotes a comparison of elements aj > ak ? One edge denotes aj < ak, the other denotes aj > ak Could ignore equals because elements are distinct Each leaf is a permutation of 3 objects that describes how the input array is sorted by the path that connects the root to that leaf.

4 Binary decision tree (BTD) to sort n distinct elements
A necessary condition for correct comparison sorting is that every permutation of n objects must appear in a decision tree as a leaf that is reachable from the root. The number of nodes in the path from root to leaf will vary for different permutations of n objects. We are interested in a lower bound on runtime of comparison sorting; so we ask, “What is the minimum height of a BDT for comparison sorting of n objects?” Define height of a tree as the number of edges in longest path from the root to a leaf.

5 What is the height of this BDT?
h = 3 is an upper bound on the number of comparisons needed to sort any permutation of 3 inputs. We are looking for a lower bound on number of comparisons A complete binary tree of height h has 2h leaves. Use this to get a lower bound.

6 Minimum height of decision tree to sort n elements
A complete binary tree of height h has 2h leaves A BDT of height h to sort n elements has n! leaves and is not complete; therefore, 2h > n! Solve for h h > lg(n!) is a lower bound on number of comparison to sort any permutation of n inputs Use Stirling’s approximation to get a simpler form of lower bound that is valid at large n. Dominate term in lg(n!) is nlgn. Therefore; number of comparisons = W(nlgn)

7 Sorting algorithms with linear runtime

8 Counting Sort Counting sort requires
(1) elements to be sorted are positive integers (2) know range of integers in the input array (i.e. no elements have a value < 0 or > k) and (2) allow us to use the elements to be sorted as array indices For every element x count the number of elements that are less than x and (2) allow us to do this without comparing the size of elements.

9 Counting sort pseudocode and example
A is input. B is output. Initialize work space Sort into B Update C

10 Order of non-distinct elements preserved

11 Runtime of Counting-sort
initialization for loop (lines 1&2) requires Q(k) 1st pass on C[i] (for loop in lines 3&4) requires Q(n) 2nd pass on C[i] (for loop in lines 6&7) requires Q(k) Building the output (for loop in lines 9,10&11) requires Q(n) Overall T(n) =Q(k + n)  Q(n) for finite k

12 Bucket sort Beats lower bound on comparison sorts by using the
distribution of input values Example: Sort elements uniformly distributed on [0,1) Define bins or buckets such that all 0.0 < x < 0.1 go in bin 0 all 0.1 < x < 0.2 go in bin 1 etc. Due to uniformly distributed [0,1), expect only a small number of elements to fall into any one bin Small number of elements can be efficiently sorted by Insertion-sort Concatenate sorted bins to get the sorted output

13 Bucket sort pseudocode and example
Note: number bins = n

14 Runtime of Bucket-sort is a random variable
Different permutations of the same input have the same runtime. Note different from comparison sorting Different instances of inputs of the same size will have different bin structure and different runtimes T(n) = Q(n) O(ni2) (bin input and insertion-sort bins) E[T(n)] = Q(n) O(E[ni2]) E[ni2] = average of the square of the number of input values in the ith bin when they are uniformly distributed on [0,1) E[ni2] = 2 – 1/n (text ) Note: no dependence on i E[T(n)] = Q(n) O(2-1/n) = Q(n) + nO(2-1/n)  Q(n)


Download ppt "Chapter 8: Overview Comparison sorts: algorithms that sort sequences by comparing the value of elements Prove that the number of comparison required to."

Similar presentations


Ads by Google