Algorithms and Data Structures
Algorithms and Data Structures
Algorithms and Data Structures
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
N.Wirth. <strong>Algorithms</strong> <strong>and</strong> <strong>Data</strong> <strong>Structures</strong>. Oberon version 70<br />
i.e., it is of order n.This explains the power of the program Find for finding medians <strong>and</strong> similar quantiles,<br />
<strong>and</strong> it explains its superiority over the straightforward method of sorting the entire set of c<strong>and</strong>idates before<br />
selecting the k-th (where the best is of order n*log(n)). In the worst case, however, each partitioning step<br />
reduces the size of the set of c<strong>and</strong>idates only by 1, resulting in a required number of comparisons of order<br />
n 2 . Again, there is hardly any advantage in using this algorithm, if the number of elements is small, say,<br />
fewer than 10.<br />
2.3.5 A Comparison of Array Sorting Methods<br />
To conclude this parade of sorting methods, we shall try to compare their effectiveness. If n denotes the<br />
number of items to be sorted, C <strong>and</strong> M shall again st<strong>and</strong> for the number of required key comparisons <strong>and</strong><br />
item moves, respectively. Closed analytical formulas can be given for all three straight sorting methods.<br />
They are tabulated in Table 2.8. The column headings min, max, avg specify the respective minima,<br />
maxima, <strong>and</strong> values averaged over all n! permutations of n items.<br />
min avg max<br />
Straight C = n-1 (n 2 + n - 2)/4 (n 2 - n)/2 - 1<br />
insertion M = 2(n-1) (n 2 - 9n -10)/4 (n 2 - 3n - 4)/2<br />
Straight C = (n 2 - n)/2 (n 2 - n)/2 (n 2 - n)/2<br />
selection M = 3(n-1) n*(ln(n) + 0.57) n 2 /4 + 3(n-1)<br />
Straight C = (n 2 -n)/2 (n 2 -n)/2 (n 2 -n)/2<br />
exchange M = 0 (n 2 -n)*0.75 (n 2 -n)*1.5<br />
Table 2.8. Comparison of straight sorting methods.<br />
No reasonably simple accurate formulas are available on the advanced methods. The essential facts are<br />
that the computational effort needed is c*n 1.2 in the case of Shellsort <strong>and</strong> is c*n*log(n) in the cases of<br />
Heapsort <strong>and</strong> Quicksort, where the c are appropriate coefficients.<br />
These formulas merely provide a rough measure of performance as functions of n, <strong>and</strong> they allow the<br />
classification of sorting algorithms into primitive, straight methods (n 2 ) <strong>and</strong> advanced or "logarithmic"<br />
methods (n*log(n)). For practical purposes, however, it is helpful to have some experimental data<br />
available that shed light on the coefficients c which further distinguish the various methods. Moreover, the<br />
formulas do not take into account the computational effort expended on operations other than key<br />
comparisons <strong>and</strong> item moves, such as loop control, etc. Clearly, these factors depend to some degree on<br />
individual systems, but an example of experimentally obtained data is nevertheless informative. Table 2.9<br />
shows the times (in seconds) consumed by the sorting methods previously discussed, as executed by the<br />
Modula-2 system on a Lilith personal computer. The three columns contain the times used to sort the<br />
already ordered array, a r<strong>and</strong>om permutation, <strong>and</strong> the inversely ordered array. Table 2.9 is for 256 items,<br />
Table 2.10 for 2048 items. The data clearly separate the n 2 methods from the n*log(n) methods. The<br />
following points are noteworthy:<br />
1. The improvement of binary insertion over straight insertion is marginal indeed, <strong>and</strong> even negative in the<br />
case of an already existing order.<br />
2. Bubblesort is definitely the worst sorting method among all compared. Its improved version<br />
Shakersort is still worse than straight insertion <strong>and</strong> straight selection (except in the pathological case of<br />
sorting a sorted array).<br />
3. Quicksort beats Heapsort by a factor of 2 to 3. It sorts the inversely ordered array with speed<br />
practically identical to the one that is already sorted.