home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Big Blue Disk 17
/
bbd17.zip
/
SORTDEMO.TXT
< prev
next >
Wrap
Text File
|
1987-12-03
|
4KB
|
83 lines
|D╔═══════════╗═══════════════════════════════════════════════════════════════════
|D║ |5Brainware |D║═══════════════════════════════════════════════════════════════════
|D╚═══════════╝═══════════════════════════════════════════════════════════════════
^C^1A Pair of Sorts
^Cby
^CJohn Sigle
This section includes animated demonstrations of two sorting methods:
InsertionSort and ShellSort. Each demo shows a PASCAL program as it executes
as well as showing the data values being manipulated. These demos can be run
at varying speeds or in single step mode (a line is executed when you press
the spacebar.) They vividly show the nature of the sorting method and relate
it directly to a PASCAL program for performing the sort.
There are over a dozen truly distinct sorting methods; to name a few:
InsertionSort, SelectionSort, BubbleSort, ShellSort, QuickSort, HeapSort,
MergeSort and RadixSort. To further complicate matters, each of these has a
handful of significant variations. So what? Well, these methods differ
significantly in their inherent efficiency. It doesn't matter which method
you use if you are only sorting 40 or 50 items. However, if you have 10,000
items or so to sort, your choice of method can literally mean the difference
between 1 minute and 1 day of time required to accomplish the sort.
InsertionSort is one of the simpler methods. As with most of the simpler
methods, its inherent efficiency is relatively poor. ShellSort, which is a
cousin of InsertionSort, is slightly more complicated but has substantially
better inherent efficiency.
InsertionSort works as follows. It consists of a number of stages. Actually
the number of stages is one less than the number of items to be sorted. At
the start of each stage a beginning portion of the list to be sorted, in
ascending order say, has already been put in order. During the stage the first
item beyond this beginning portion is moved back within the beginning portion
to the position where it belongs, thereby increasing the size of this (sorted)
beginning portion.
Try the InsertionSort demo to see how it works. You can control the speed
by pressing a digit 0-9 (not the numeric keypad) at any time. Nine is the
highest speed while 1 is the lowest. A speed of 0 puts you in single-step
mode. In that mode you press the spacebar twice to execute a line of the
program. At any time you can scroll the program display forward or backward
by means of the up or down arrows. At the conclusion of the demo the number
of comparisons, swaps and assignments performed is reported.
ShellSort, invented by D. L. Shell, is only a few lines longer than
InsertionSort and is very strongly related to it. It involves the use of
InsertionSort several times on various parts of the list to be sorted.
ShellSort proceeds in several phases. For each phase there is an associated
distance value. This distance value is reduced with each successive phase,
with the last phase having a distance value of 1. During each phase of
ShellSort, InsertionSort is used a number of times to sort a portion of the
list. The trick is that each portion does not consist of consecutive items of
the list but rather of items separated by the distance value associated with
the phase. Since the last phase uses a distance of 1 it is exactly an
InsertionSort of the entire list. However, by the time this last phase occurs
the items are almost in order and relatively few comparisons and swaps are
required.
The secret of ShellSort's efficiency is the fact that an item can make a
large move toward the position where it belongs on the basis of a single
comparison. This is true of all the more efficient sorting methods. It is
not true for InsertionSort where an item can only move one position on the
basis of one comparison.
Try the ShellSort demo to see ShellSort in action.
Both of these demos can be accessed by selecting "Run It" for this item,
and picking which one to run when asked. The source code, in Turbo Pascal,
for the sorting algorithms of each is in the files INSERT.PAS and SHELL.PAS
respectively.
To run this program outside BIG BLUE DISK, type ^1PASRUN SORTDEMO^0.
DISK FILES THIS PROGRAM USES:
^FSORTDEMO.CHN
^FINSERTC.CHN
^FSHELLC.CHN
^FPASRUN.COM
^FRETURN.CHN
^FINSERT.PAS
^FSHELL.PAS