0% found this document useful (0 votes)
67 views

Minimum and Maximum

Here are solutions to the problems: 1. Use SELECT to find the median m of S in O(n) time. Then partition S into elements less than or equal to m and elements greater than m. The k elements closest to the median will be some from each partition. 2. Binary search on possible medians in the range [min(X[1],Y[1]), max(X[n],Y[n])]. For each candidate median c, count numbers less than or equal to c in X and Y using binary search in the arrays. If the count is between n/2 and 3n/2, c is the median. Each count takes O(log n) time, and there are

Uploaded by

Dino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Minimum and Maximum

Here are solutions to the problems: 1. Use SELECT to find the median m of S in O(n) time. Then partition S into elements less than or equal to m and elements greater than m. The k elements closest to the median will be some from each partition. 2. Binary search on possible medians in the range [min(X[1],Y[1]), max(X[n],Y[n])]. For each candidate median c, count numbers less than or equal to c in X and Y using binary search in the arrays. If the count is between n/2 and 3n/2, c is the median. Each count takes O(log n) time, and there are

Uploaded by

Dino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Minimum and Maximum

Problem: Find the maximum and the minimum of n elements.

• Naïve algorithm 1: Find the minimum, then find the


maximum -- 2(n-1) comparisons.
• Naïve algorithm 2: Find the minimum, then find the
maximum of n-1 elements -- (n-1) + (n-2) = 2n -3
comparisons.
Minimum and Maximum – better algorithms

Problem: Find the maximum and the minimum of n elements.

Approach 1
•Sort n/2 pairs. Find min of losers, max of winners.
# comparisons: n/2 + n/2 –1 + n/2-1 = 3n/2 –2.
Is this the best possible?
Approach 2
•Divide into n/2 pairs. Compare the first pair, set winner to
current max, loser to current min.
•Sort next pair, compare winner to current max, loser to
current min.
#comparisons: 1 + 3(n/2 –1) = 3n/2 –2.
Lower bounds for the MIN and MAX

Claim: Every comparison-based algorithm for finding


both the minimum and the maximum of n elements
requires at least (3n/2)-2 comparisons.
Idea: Use similar argument as for the minimum
Max = maximum and Min=minimum only if:
Every element other than min has won at least 1
Every element other than max has lost at least 1
A proof?

“Proof” from the web: For each comparison, x<y, score a point
if this is first comparison that x loses or if y wins and 2
points if both occur. Before the algorithm can terminate n-2
must both win and lose (since they aren't min or max) and 2
elements must either win or lose. Thus, 2(n-2)+2 points are
scored before termination.

Define A to be the set of elements that have not won or lost


a comparison. All comparisons between elements in A must
score 2 points. All other comparisons can score at most 1
point. Let X be A-A comparisons. Let Y be number of other
comparisons. We want to minimize X+Y such that 2X+Y 
2n-2 & X  n/2 (assume n is even). Given the constraints
we want to make X as big as possible. So set X=n/2. Then
Y  2n-2-2X  Y  2n-2-n  Y  n-2  X+Y  n/2 + n - 2.
Is the previous proof correct?
Lower bounds for the MIN and MAX

Idea: Define 4 sets:

U: has not participated in a comparison


W: has won all comparisons
L: has lost all comparisons
N: has won and lost at least one comparison

Note: All these sets are disjoint.

1. Initially all elements in U.


2. Finally no elements in U, 1 each in W,L and n-2 in N.
3. Each element in N comes from U via W or L.
Lower bounds for the MIN and MAX - contd

Idea: Score a point when an element enters W or L or N for


the first time.
Question: Can we ensure that only U-U comparisons result in
two points being scored?
Answer: YES! The adversary argument!

The adversary constructs a worst-case input by revealing as


little as possible about the inputs.
Lower bounds for the MIN and MAX - contd

Adversary strategy:
U-U: any
U-W: make element of W winner
U-L: make element of L loser
U-N: any
W-W: any (be consistent with before)
W-L/N: make element of W winner
L-L: any (be consistent with before)
L-N: make element of L loser
Lower bounds for the MIN and MAX – contd.

We need to score 2n–2 points. At most n/2 U-U comparisons


can be made – gives n points.
To move n-2 elements to N, we need another n-2
comparisons.
Next: Linear sorting

Q: Can we beat the (n log n) lower bound for


sorting?
A: In general no, but in some special cases
YES!

Ch 7: Sorting in linear time


Non-Comparison Sort – Bucket Sort

• Assumption: uniform distribution


– Input numbers are uniformly distributed in [0,1).
– Suppose input size is n.
• Idea:
– Divide [0,1) into n equal-sized subintervals (buckets).
– Distribute n numbers into buckets
– Expect that each bucket contains few numbers.
– Sort numbers in each bucket (insertion sort as
default).
– Then go through buckets in order, listing elements
Can be shown to run in linear-time on average
Example of BUCKET-SORT
Bucket Sort - generalizations

• What if input numbers are NOT uniformly


distributed?
• What if the distribution is not known a priori?
Non-Comparison Sort – Counting Sort

• Assumption: n input numbers are integers in the


range [0,k], k=O(n).
• Idea:
– Determine the number of elements less than
x, for each input x.
– Place x directly in its position.
Counting Sort - pseudocode

Counting-Sort(A,B,k)
• for i0 to k
• do C[i] 0
• for j 1 to length[A]
• do C[A[j]] C[A[j]]+1
• // C[i] contains number of elements equal to i.
• for i 1 to k
• do C[i]=C[i]+C[i-1]
• // C[i] contains number of elements  i.
• for j length[A] downto 1
• do B[C[A[j]]] A[j]
• C[A[j]] C[A[j]]-1
Counting Sort - example
Counting Sort - analysis
1. for i0 to k (k)
2. do C[i] 0 (1)
3. for j 1 to length[A] (n)
4. do C[A[j]] C[A[j]]+1 (1) ((1) (n)= (n))
5. // C[i] contains number of elements equal to i. (0)
6. for i 1 to k (k)
7. do C[i]=C[i]+C[i-1] (1) ((1) (n)= (n))
8. // C[i] contains number of elements  i. (0)
9. for j length[A] downto 1 (n)
10. do B[C[A[j]]] A[j] (1) ((1) (n)= (n))
11. C[A[j]] C[A[j]]-1 (1) ((1) (n)= (n))

Total cost is (k+n), suppose k=O(n), then total cost is (n).


So, it beats the (n log n) lower bound!
Stable sort

• Preserves order of elements with the same


key.
• Counting sort is stable.

Crucial question: can counting sort be used to


sort large integers efficiently?
Radix sort

Radix-Sort(A,d)
• for i1 to d
• do use a stable sort to sort A on digit i

Analysis:
Given n d-digit numbers where each digit takes on
up to k values, Radix-Sort sorts these numbers
correctly in (d(n+k)) time.
Radix sort - example

1019 2231 1019 1019 1019


3075 3075 2225 3075 2225
Sorted!
2225 2225 2231 2225 2231
2231 1019 3075 2231 3075

1019 1019
3075 2231
Not
2231 2225 sorted!
2225 3075
Next: Medians and Order Statistics (Ch. 9)

Order statistics: The ith order statistic of n elements


S={a1, a2,…, an} : ith smallest elements
•Minimum and maximum, Median
•finding the kth largest element in an unsorted array.
Already seen:
1. k=1: (n) algorithm optimal.
2. Also, Heapify + Extract-max: (n) algorithm.
Same bounds hold for any constant k.
3. Sorting solves it for any k. (n log n) algorithm.

What about k=n/2? Can we do better than (n log n)


algorithm?
Medians and Order Statistics

To select the ith smallest element of S={a1, a2,…, an}


• Can we use PARTITION?
•if we are very lucky, we will get it in the first try!
•otherwise we should have a smaller set to recurse on.

• No guarantee of being lucky!


How can we guarantee a significantly smaller set?

The algorithm is the most complicated divide-and-


conquer algorithm in this course!
Order Statistics

1. Divide n elements into n/5 groups of 5 elements.


2. Find the median of each group.
3. Use SELECT recursively to find the median x of the
above n/5 medians.
4. Partition using x as pivot, and find position k of x.
5. If i=k return
else recurse on the appropriate subarray.

What kind of split does this produce?


The Way to Select x
At least (3n/10)-6 elements <x Divide elements into n/5 groups
of 5 elements each.
Find the median of each group
Find the median of the medians

At least (3n/10)-6 elements >x


Analysis of SELECT
• Steps 1,2,4 take O(n),
• Step 3 takes T(n/5).
• Let us see step 5:
- At least half of medians in step 2 are  x, thus at least
1/2 n/5  -2 groups contribute 3 elements which are  x.
i.e, 3(1/2 n/5  -2)  (3n/10)-6.
- Similarly, the number of elements  x is also at least
(3n/10)-6.
– Thus, |S1| is at most (7n/10)+6, similarly for |S3|.
– Thus SELECT in step 5 is called recursively on at most
(7n/10)+6 elements.
• Recurrence is:
T(n)= O(1) if n< 140
T(n/5)+T(7n/10+6)+O(n) if n 140
Solve recurrence by substitution

• Suppose T(n)  cn, for some c.


• T(n)  c n/5+ c(7n/10+6) + an
 cn/5+ c + 7/10cn+6c + an
= 9/10cn+an+7c
=cn+(-cn/10+an+7c)
– Which is at most cn if -cn/10+an+7c<0.
– i.e., c 10a(n/(n-70)) when n>70.

– So select n=140, and then c 20a.


Note: n may not be 140, any integer >70 is OK.
Implication for Quicksort

• Worst case improves to O(n log n)


BUT…
Test your understanding

1. Problem 9.3-7: Describe an O(n) algorithm


that, given a set S of n distinct numbers and a
positive integer k <= n, determines the k
numbers in S that are closest to the median of
S.
2. Problem 9.3-8: Let X[1..n], Y[1..n] be two
sorted arrays. Give an O(lg n) algorithm to
find the median of all 2n elements in arrays
X,Y.

You might also like