CS 253: Algorithms. Chapter 8 Sorting in Linear Time. Credit : Dr. George Bebis. How Fast Can We Sort?. How Fast Can We Sort?. Insertion sort: O(n 2 ) Bubble Sort, Selection Sort : (n 2 ) Merge sort : ( nlgn )
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Sorting in Linear Time
Credit: Dr. George Bebis
ai < aj, ai≤aj, ai = aj, ai≥aj, orai>aj
to determine the relative order of aiandaj
To sort n elements, comparison sorts must make (nlgn)comparisons in the worst case.
(i.e., the height of the decision tree)
Any binary tree of height hhas at most 2h leaves
Proof:by induction on h
Basis:h = 0 tree has one node, which is a leaf
# of Leaves = 1 ≤ 20 (TRUE)
Inductive step:assume true for h-1 (i.e. #Leaves ≤ 2h-1)
No. of leaves at level h = 2 (no. of leaves at level h-1)
≤ 2 2h-1
Theorem:Any comparison sort algorithm requires
(nlgn)comparisons in the worst case.
Proof: How many leaves does the tree have?
n! ≤ 2h
h ≥ lg(n!) = (nlgn)
(see next slide)
Exercise 8.1-1: What is the smallest possible depth of a leaf in a decision tree for a comparison sort?
n ≤lg(n!) ≤ nlgn
Find the number of times A[i] appears in A
Allocate C[1..r] (histogram)
For 1 ≤ i ≤ n, ++C[A[i]}
Find the number of elements ≤ A[i]
(i.e. cumulative sums)
Alg.: COUNTING-SORT(A, B, n, r)
% C[i] contains the number of elements =i ; frequencies
% C[i] contains the number of elements ≤i ; cumulative sum
Alg.: COUNTING-SORT(A, B, n, k)
Overall time: (n + r)
running time is (n)
e.g. key = x1x2...xdwhere 0 ≤ xi ≤ k-1
key10 = 15, d=2, k=10 where 0 ≤ xi ≤ 9
key2 = 1111, d=4, k=2 where 0 ≤ xi ≤ 1
until all digits have been sorted
Alg.: RADIX-SORT(A, d)
for i ← 1 to d
do use a stable sort to sort array A on digit i
How do things go wrong if an unstable sorting alg. is used?
assuming that we use counting sort
Since d=Θ(1) and k =O(n)
Therefore, Radix Sort runs in (n) time
You are given 5 distinct numbers to sort.
Describe an algorithm which sorts them using at most 6comparisons, or argue that no such algorithm exists.
Total # of leaves in the comparison tree = 5!
If the height of the tree is h, then (total # of leaves ≤ 2h)
2h ≥ 5!
h ≥ log2(5!)
h > 6
There is at least one input permutation which will require at least 7 comparisons to sort. Therefore, no such algorithm exists.