Documentos de Académico
Documentos de Profesional
Documentos de Cultura
1
August 4, 2008 Section 0
Contents
1 Algorithm 3
2 Time complexity 3
2
August 4, 2008 Section 2
Abstract
In this lecture, we examine a selection algorithm whose running time is O(n)
in the worst case. Like the previous algorithm in which the list is partitioned
randomly and the ith minimum is found recursively, the algorithm SELECT also
finds the desired element by recursively partitioning the input array. However the
partition in this case is not done randomly but in such a way so as to guarantee a
good split.
1 Algorithm
Algorithm : SELECT
1. The n elements of the list L[1, 2, ...., n] are grouped into ⌈n/5⌉ groups of 5 elements
each and at most one group having the remaining n mod 5 elements.
2. The median of each of the ⌈n/5⌉ groups of elements is determined.
For determining the median, we check each element in the group. If for a particular
element, there are exactly 2 elements less than it in the group (and hence exactly 2 ele-
ments greater than it,considering distinct elements) then it is the median of that group.
3. Using SELECT recursively, we find the median µ of the ⌈n/5⌉ medians found in
previous step.µ is called the median-of-medians.
In case of even number of elements we consider the larger one (of the two medians) as
the median.
4. The input list L[1.....n] is partitioned into lists L1 and L2 using the median-of-medians
µ as the pivot element.Let k be one more than the number of elements on the low side
of the partition (L1 ), so that µ is the kth smallest element and there are n - k elements
on the high side of the partition(L2 ).
5. If i = k, then µ is the ith minimum. Hence, return µ. Otherwise, use SELECT
recursively to find the ith smallest element on the low side (L1 ) if i < k, or the (i - k)th
smallest element on the high side(L2 ) if i > k.
2 Time complexity
For calculating the time complexity of the algorithm, we need to find the number of
comparisions involved in each step of the algorithm.
In step 1, since the group is in-place divided into ⌈n/5⌉ groups the number of compari-
sions is O(⌈n/5⌉).
In step 2, the number of comparisions invoved in finding the median of a particular group
is O(1) as the number of elements in the group is always ≤ 5 (constant) and since there
are ⌈n/5⌉ such groups the total number of comparisions = ⌈n/5⌉ * O(1).
3
August 4, 2008 Section 2
4
August 4, 2008 Section 2
Step # comparisions
1. Group the n elments into ⌈n/5⌉ groups O(⌈n/5⌉)
2. Find the medians of the groups ⌈n/5⌉ * O[1] = O(⌈n/5⌉)
3. Recursively find µ T(⌈n/5⌉)
4. Partition the list L into L1 and L2 O(n)
5. Recurse on L1 and L2 to find the ith minimum ≤ T(7⌈n/10⌉ + 6)
⇒ if nc/10 ≥ 14c
⇒ if n ≥ 140.
which is possible by considering a sufficiently large value of c.