Download presentation
Presentation is loading. Please wait.
Published byUfuk Ünal Modified over 5 years ago
1
Binhai Zhu Computer Science Department, Montana State University
Linear Time Selection Binhai Zhu Computer Science Department, Montana State University Frequently, presenters must deliver material of a technical nature to an audience unfamiliar with the topic or vocabulary. The material may be complex or heavy with detail. To present technical material effectively, use the following guidelines from Dale Carnegie Training®. Consider the amount of time available and prepare to organize your material. Narrow your topic. Divide your presentation into clear segments. Follow a logical progression. Maintain your focus throughout. Close the presentation with a summary, repetition of the key steps, or a logical conclusion. Keep your audience in mind at all times. For example, be sure data is clear and information is relevant. Keep the level of detail and vocabulary appropriate for the audience. Use visuals to support key points or steps. Keep alert to the needs of your listeners, and you will have a more receptive audience. 8/2/2019
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). 8/2/2019
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 A 16 14 10 1 7 9 3 2 5 8 8/2/2019 p r
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 A 16 14 10 1 7 9 3 2 5 8 8/2/2019 p r
5
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 The ideal pivot is exactly the n/2-th smallest element! 1 2 3 4 5 6 7 8 9 10 A 16 14 10 1 7 9 3 2 5 8 8/2/2019 p r
6
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 The ideal pivot is exactly the └n/2┘-th smallest element! When n is even, └n/2┘-th When n is odd, (n+1)/2-th 1 2 3 4 5 6 7 8 9 10 A 16 14 10 1 7 9 3 2 5 8 8/2/2019 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 A 1 7 10 14 16 2 3 5 8 9 8/2/2019
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 A 1 7 10 14 16 2 3 5 8 9 8/2/2019
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 A 1 7 10 14 16 2 3 5 8 9 8/2/2019 ↑
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 A 1 2 3 5 16 7 10 9 8 14 8/2/2019 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 A 1 2 3 5 16 7 10 9 8 14 8/2/2019 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 8/2/2019
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? 8/2/2019
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? 8/2/2019
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)! 8/2/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.