


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Lower Bounds for Comparison Sorting
Sorting returns a permutation of its input. There are n! permutations of n elements.
ai ≤ aj determines if ai is before/after aj. ai ≤ aj chooses between two sets of permuta- tions.
Map to a binary decision tree. Map each comparison to a node in a tree. Map sets of permutations to subtrees. Map singleton sets to leaves.
A binary tree with height h has ≤ 2 h^ leaves. A binary tree with n! leaves has h ≥ lg(n!) lg(n!) ∈ Θ(n lg n), so h ∈ Ω(n lg n). Worst-case of comparison sorting is Ω(n lg n).
≤ 2 d^ leaves at depth ≤ d. if d < lg(n!) − 1, then 2d^ < n!/ 2 ≥ n!/2 leaves have d ≥ lg(n!) − 1 Average-case of comparison sorting is Ω(n lg n).
Counting-Sort is Θ(n + k) and stable. It assumes that each A[j] ∈ { 1 , 2 ,... , k}.
Counting-Sort(A, B, k) C ← an array of k zeros for j ← 1 to length[A] do C[A[j]] ← C[A[j]] + 1 C[i] is the number of elements equal to i for i ← 2 to k do C[i] ← C[i] + C[i − 1] C[i] is the number of elements ≤ i for j ← length[A] downto 1 do B[C[A[j]]] ← A[j] C[A[j]] ← C[A[j]] − 1
C = (1, 2 , 1) after first loop C = (1, 3 , 4) after second loop j = 4, B = ( 2 , 2 , 2 , 2 ), C = (1, 2 , 4) j = 3, B = (1, 2 , 2 , 2 ), C = (0, 2 , 4) j = 2, B = (1, 2 , 2 , 2 ), C = (0, 1 , 4) j = 1, B = (1, 2 , 2 , 3), C = (0, 1 , 3)
Bucket-Sort is Θ(n) on average. Assumes uniform distribution over the interval [0, 1)
Bucket-Sort(A) n ← length[A] B ← an array of n empty lists for i ← 1 to n do insert A[i] into list B[bn A[i]c] for i ← 0 to n − 1 do sort list B[i] with Insertion-Sort concatenate the lists B[0] to B[n − 1]
Average Case: There are n elements and n buckets. Let ni be number of elements in bucket i. Insertion-Sort on ni elements is O(n^2 i ). Need to bound E[n^2 i ] (expected value of n^2 i ). ni is binomial with prob. p = 1/n and n trials. E[ni] = np = 1 Var[ni] = np(1 − p) = 1 − 1 /n Var[ni] = E[n^2 i ] − E[ni]^2 implies E[n^2 i ] < 2 Expected time of second loop is Θ(n).