150 likes | 171 Views
Learn about selecting the ith smallest element efficiently in an array using the selection method, improving on standard sorting.
E N D
Linear Time Selection Binhai Zhu Computer Science Department, Montana State University
Definition • Given an unsorted array A[1..n], return the i-th smallest (largest) element of A (1) How about the smallest and largest elements? (2) With sorting, it is easy (but costs too much).
Think of Quicksort Quicksort(A,p,r) (1) if p < r (2) then q ← partition(A,p,r) (3) Quicksort(A,p,q-1) (4) Quicksort(A,q+1,r) 1 2 3 4 5 6 7 8 9 10 3 2 5 8 A 16 14 10 1 7 9 r p
What about partition? partition(A,p,r) (1) x ← A[r] (2) i ← p - 1 (3) for j ← p to r-1 (4) if A[j] ≤ x (5) then i ← i + 1 (6) exchange A[i] ↔ A[j] (7) exchange A[i+1] ↔ A[r] (8) return i+1 1 2 3 4 5 6 7 8 9 10 1 A 16 14 10 7 9 3 2 5 8 p r
What about partition? The ideal pivot is exactly the n/2-th smallest element! partition(A,p,r) (1) x ← A[r] (2) i ← p - 1 (3) for j ← p to r-1 (4) if A[j] ≤ x (5) then i ← i + 1 (6) exchange A[i] ↔ A[j] (7) exchange A[i+1] ↔ A[r] (8) return i+1 1 2 3 4 5 6 7 8 9 10 1 A 16 14 10 7 9 3 2 5 8 p r
What about partition? The ideal pivot is exactly the └n/2┘-thsmallest element! When n is even, └n/2┘-th When n is odd, (n+1)/2-th partition(A,p,r) (1) x ← A[r] (2) i ← p - 1 (3) for j ← p to r-1 (4) if A[j] ≤ x (5) then i ← i + 1 (6) exchange A[i] ↔ A[j] (7) exchange A[i+1] ↔ A[r] (8) return i+1 1 2 3 4 5 6 7 8 9 10 1 A 16 14 10 7 9 3 2 5 8 p r
Select(A,i) (1) Partition A into groups of 5 elements, sort each group. 1 2 3 4 5 6 7 8 9 10 7 9 A 1 10 14 16 2 3 5 8
Select(A,i) (1) Partition A into groups of 5 elements, sort each group. (2) While sorting each group, identify the medians as well. Collect the ┌n/5┐medians into M. 1 2 3 4 5 6 7 8 9 10 7 9 A 1 10 14 16 2 3 5 8
Select(A,i) (1) Partition A into groups of 5 elements, sort each group. (2) While sorting each group, identify the medians as well. Collect the ┌n/5┐medians into M. (3) Recursively call Select(M,└|M|/2┘) to find the median x of M. 1 2 3 4 5 6 7 8 9 10 7 9 A 1 10 14 16 2 3 5 8 ↑
Select(A,i) (1) Partition A into groups of 5 elements, sort each group. (2) While sorting each group, identify the median as well. Collect the ┌n/5┐medians into M. (3) Recursively call Select(M,└|M|/2┘) to find the median x of M. (4) Partition A around the pivot x (index of x is k). 1 2 3 4 5 6 7 8 9 10 3 5 A 1 2 16 7 10 9 8 14 k=4
Select(A,i) (1) Partition A into groups of 5 elements, sort each group. (2) While sorting each group, identify the median as well. Collect the ┌n/5┐medians into M. (3) Recursively call Select(M,└|M|/2┘) to find the median x of M. (4) Partition A around the pivot x (index of x is k). (5) if i=k, return x; if i < k, return Select(A[1..k-1],i); if i > k, return Select(A[k+1..n],i-k). 1 2 3 4 5 6 7 8 9 10 3 5 14 A 1 2 16 7 10 9 8 k=4
Cost Let T(n) be the running time for select(A,i). T(n) ≤ O(1), if n<140 T(n) ≤ T(┌n/5┐) + T(7n/10+6) + O(n), if n ≥ 140
Cost Let T(n) be the running time for select(A,i). T(n) ≤ O(1), if n<140 T(n) ≤ T(┌n/5┐) + T(7n/10+6) + O(n), if n ≥ 140 Why?
Cost Let T(n) be the running time for select(A,i). T(n) ≤ O(1), if n<140 T(n) ≤ T(┌n/5┐) + T(7n/10+6) + O(n), if n ≥ 140 How to solve this recurrence relation?
Cost Let T(n) be the running time for select(A,i). T(n) ≤ O(1), if n<140 T(n) ≤ T(┌n/5┐) + T(7n/10+6) + O(n), if n ≥ 140 T(n) ≤ cn = O(n)!