Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu Thuật toán Algorithms (Phần 11) doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (76.62 KB, 10 trang )

ELEMENTARY SORTING METHODS
93
small index to each key before sorting or lengthening the sort key in some
other way. It is easy to take stability for granted: people often react to the
unpleasant effects of instability with disbelief. Actually there are few methods
which achieve stability without using significant extra time or space.
The following program, for sorting three records, is intended to illustrate
the general conventions that we’ll be using. (In particular, the main program is
a peculiar way to exercise a program that is known to work only for N = 3: the
point is that most of the sorting programs we’ll consider could be substituted
for sort3 in this “driver” program.)
program output);

var a: array of integer;
N, i: integer;
procedure
var : integer;
begin
if then
begin
if then
end
begin end;
if then
begin end;
end;
begin
(N) ;
for to N do read(a[i]);
if then
for to N do


wri
end.
The three assignment statements following each if actually implement an
“exchange” operation. We’ll write out the code for such exchanges rather than
use a procedure call because they’re fundamental to many sorting programs
and often fall in the inner loop.
In order to concentrate on issues, we’ll work with algorithms
that simply sort arrays of integers into numerical order. It is generally straight-
forward to adapt such algorithms for use in a practical application involving
large keys or records. Basically, sorting programs access records in one of two
ways: either keys are accessed for comparison, or entire records are accessed
94
CHAPTER 8
to be moved. Most of the algorithms that we will study can be recast in terms
of performing these two operations on arbitrary records. If the records to be
sorted are large, it is normally wise to do an “indirect sort”: here the records
themselves are not necessarily rearranged, but rather an array of pointers (or
indices) is rearranged so that the first pointer points to the smallest record,
etc. The keys can be kept either with the records (if they are large) or with
the pointers (if they are small).
By using programs which simply operate on a global array, we’re ignoring
“packaging problems” that can be troublesome in some programming environ-
ments. Should the array be passed to the sorting routine as a parameter?
Can the same sorting routine be used to sort arrays of integers and arrays
of reals (and arrays of arbitrarily complex records)? Even with our simple
assumptions, we must (as usual) circumvent the lack of dynamic array sizes
in Pascal by predeclaring a maximum. Such concerns will be easier to deal
with in programming environments of the future than in those of the past
and present. For example, some modern languages have quite well-developed
facilities for packaging together programs into large systems. On the other

hand, such mechanisms are not truly required for many applications: small
programs which work directly on global arrays have many uses; and some
operating systems make it quite easy to put together simple programs like
the one above, which serve as “filters” between their input and their output.
Obviously, these comments apply to many of the other algorithms that we’ll
be examining, though their effects are perhaps most acutely felt for sorting
algorithms.
Some of the programs use a few other global variables. Declarations
which are not obvious will be included with the program code. Also, we’ll
sometimes assume that the array bounds go to 0 or to hold special keys
used by some of the algorithms. We’ll frequently use letters from the alphabet
rather than numbers for examples: these are handled in the obvious way using
Pascal’s ord and “transfer functions” between integers and characters.
The sort3 program above uses an even more constrained access to the file:
it is three instructions of the form “compare two records and exchange them
if necessary to put the one with the smaller key first.” Programs which use
only this type of instruction are interesting because they are well suited for
hardware implementation. We’ll study this issue in more detail in Chapter
35.
Selection Sort
One of the simplest sorting algorithms works as follows: first find the smallest
element in the array and exchange it with the element in the first position,
then find the second smallest element and exchange it with the element in
ELEMENTARY SORTING METHODS
95
the second position, continuing in this way until the entire array is sorted.
This method is called selection sort because it works by repeatedly “selecting”
the smallest remaining element. The following program sorts a into
numerical order:
procedure

selection;
i, j, min, integer;
begin
for to N do
begin
min:=i;
for to N do
if then min:=j;
t:=a[min]; a[min]:=a[i];
end
end ;
This is among the simplest of sorting methods, and it will work very well for
small files. Its running time is proportional to the number of comparisons
between array elements is about since the outer loop (on i) is executed N
times and the inner loop (on j) is executed about N/2 times on the average. It
turns out that the statement min:=j is executed only on the order of N log N
times, so it is not part of the inner loop
Despite its simplicity, selection sort has a quite important application:
it is the method of choice for sorting files with very large records and small
keys. If the records are M words long (but the keys are only a few words long),
then the exchange takes time proportional to M, so the total running time
is proportional to (for the comparisons) plus NM (for the exchanges). If
M is proportional to N then the running time is linear in the amount of data
input, which is difficult to beat even with an advanced method. Of course if
it is not absolutely required that the records be actually rearranged, then an
“indirect sort” can be used to avoid the NM term entirely, so a method which
uses less comparisons would be justified. Still selection sort is quite attractive
for sorting (say) a thousand records on one-word keys.
Insertion Sort
An algorithm almost as simple as selection sort but perhaps more flexible is

insertion sort. This is the method often used by people to sort bridge hands:
consider the elements one at a time, inserting each in its proper place among
those already considered (keeping them The element being considered
is inserted merely by moving larger elements one position to the right, then
96
8
inserting the element into the vacated position. The code for this algorithm
is straightforward:
procedure insertion;
var i, j, v: integer;
begin
for to do
begin

while do
begin end;

end ;
end
As is, this code doesn’t work, because the while will run past the left end
of the array if is the smallest element in the array. One way to fix this is
to put a “sentinel” key in making it at least as small as the smallest
element in the array. Using sentinels in situations like this is common in
sorting programs to avoid including a test (in this case which almost
always succeeds within the inner loop. If for some reason it is inconvenient to
use a sentinel and the array really must have the bounds then standard
Pascal does not allow a clean alternative, since it does not have a “conditional”
and instruction: the test while and won’t work because
even when the second part of the and will be evaluated and will cause
an array access. A out of the loop seems to be required.

(Some programmers prefer to some lengths to avoid instructions,
for example by performing an action within the loop to ensure that the loop
terminates. In this case, such a solution seems hardly justified, since it makes
the program no clearer, and it adds extra overhead everytime through the
loop to guard against a rare event.)
On the average, the inner loop of insertion sort is executed about
times: The “average” insertion goes about halfway into a of size N/2.
This is inherent in the method. The point of insertion can be found more
efficiently using the searching techniques in Chapter 14, but moves (to
make room for each element being inserted) are still required; or the number
of moves can be lowered by using a linked list instead of an array, but then
the methods of Chapter 14 don’t apply and comparisons are required
(to find each insertion point).
ELEMENTARY SORTING METHODS
97
Shellsort
Insertion sort is slow because it only adjacent elements. For ex-
ample, if the smallest element happens to be at the end of the array, it takes
N steps to get it where it belongs. Shellsort is a simple extension of insertion
sort which gets around this problem by allowing exchanges of elements that
are far apart.
If we replace every occurrence of “1” by (and “2” by in
insertion sort, the resulting program rearranges a file to give it the property
that taking every hth element (starting anywhere) yields a sorted file. Such a
file is said to be h-sorted. Put another way, an h-sorted file is h independent
sorted files, interleaved together. By h-sorting for some large values of h, we
can move elements in the array long distances and thus make it easier to h-sort
for smaller values of h. Using such a procedure for any sequence of values of
h which ends in 1 will produce a sorted file: this is Shellsort.
The following example shows how a sample file of fifteen elements is

sorted using the increments 13, 4, 1:
1
2 3
4 5 6 7 8 9 10 11 12 13 14 15
A S 0 R T I N E X A M P L E
13
A E 0 R T
I N EXAMPLS
4
A E A G E I N M P L 0 R T X S
1
AAEEGI L N 0 P R S T X
In the first pass, the A in position 1 is compared to the L in position 14, then
the S in position 2 is compared (and exchanged) with the E in position 15. In
the second pass, the A T E P in positions 1, 5, 9, and 13 are rearranged to
put A E P T in those positions, and similarly for positions 2, 6, 10, and 14,
etc. The last pass is just insertion sort, but no element has to move very far.
The above description of how Shellsort gains efficiency is necessarily
imprecise because no one has been able to analyze the algorithm. Some
sequences of values of h work better than others, but no explanation for this
has been discovered. A sequence which has been shown empirically to do well
is . . . as in the following program:

×