25.11.2014 Views

Algorithms and Data Structures

Algorithms and Data Structures

Algorithms and Data Structures

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 68<br />

<strong>and</strong> it will use an implicit stack for this purpose.) The remedy lies in stacking the sort request for the longer<br />

partition <strong>and</strong> in continuing directly with the further partitioning of the smaller section. In this case, the size of<br />

the stack M can be limited to log(n).<br />

The change necessary is localized in the section setting up new requests. It now reads<br />

IF j - L < R - i THEN<br />

IF i < R THEN (*stack request for sorting right partition*)<br />

INC(s); low[s] := i; high[s] := R<br />

END;<br />

R := j (*continue sorting left partition*)<br />

ELSE<br />

IF L < j THEN (*stack request for sorting left parition*)<br />

INC(s); low[s] := L; high[s] := j<br />

END;<br />

L := i (*continue sorting right partition*)<br />

END<br />

2.3.4 Finding the Median<br />

The median of n items is defined as that item which is less than (or equal to) half of the n items <strong>and</strong><br />

which is larger than (or equal to) the other half of the n items. For example, the median of<br />

16 12 99 95 18 87 10<br />

is 18. The problem of finding the median is customarily connected with that of sorting, because the obvious<br />

method of determining the median is to sort the n items <strong>and</strong> then to pick the item in the middle. But<br />

partitioning yields a potentially much faster way of finding the median. The method to be displayed easily<br />

generalizes to the problem of finding the k-th smallest of n items. Finding the median represents the special<br />

case k = n/2.<br />

The algorithm invented by C.A.R. Hoare [2-4] functions as follows. First, the partitioning operation of<br />

Quicksort is applied with L = 0 <strong>and</strong> R = n-1 <strong>and</strong> with a k selected as splitting value x. The resulting index<br />

values i <strong>and</strong> j are such that<br />

1. a h < x for all h < i<br />

2. a h > x for all h > j<br />

3. i > j<br />

There are three possible cases that may arise:<br />

1. The splitting value x was too small; as a result, the limit between the two partitions is below the desired<br />

value k. The partitioning process has to be repeated upon the elements a i ... a R (see Fig. 2.9).<br />

≤<br />

≥<br />

L<br />

j i<br />

k<br />

R<br />

Fig. 2.9. Bound x too small<br />

2. The chosen bound x was too large. The splitting operation has to be repeated on the partition a L ... a j<br />

(see Fig. 2.10).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!