1 / 15

Linear Time Selection

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).

lhinton
Télécharger la présentation

Linear Time Selection

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linear Time Selection Binhai Zhu Computer Science Department, Montana State University

  2. 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).

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  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

  9. 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 ↑

  10. 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

  11. 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

  12. 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

  13. 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?

  14. 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?

  15. 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)!

More Related