home *** CD-ROM | disk | FTP | other *** search
-
-
-
- SORT.COM is program shell designed to allow comparison of a
- number of sorting algorithms. SORT.PAS is the corresponding
- Turbo Pascal source file. The program menu allows the user to
- generate a desired number of random integers (up to 2000, but
- tree sort can handle only 1600). The master list can be
- presorted if desired, and display of the numbers can be toggled
- on or off. The other menu choices allow the user to test the
- particular sorting methods.
-
- The individual sorting algorithms appear as named procedures
- within SORT.PAS. They were written to gain understanding of the
- sorts, and were optimized for clear expression rather than
- versatility or speed.
-
- To compare the speed of the various algorithms, I timed the
- interval between my response to the prompt "Ready?" and the
- moment when the sorted list began to print out. I tested each
- sort on 400 and 800 random integers, and then on the same number
- sets after presorting, with the following results (times in
- seconds):
-
- SORT 400 800 400 (PS) 800 (PS)
-
- bubble 29.7 114.8 .3 .3
- count 17.8 70.2 18.0 71.0
- insert 11.5 42.2 .4 .4
- select 9.2 35.6 9.1 35.4
- tree 6.3 14.1 6.3 14.0
- shell 1.9 4.0 1.1 2.1
- heap 1.6 3.5 1.7 3.6
- quick1 1.1 2.1 8.7 failed
- quick2 1.0 2.0 .7 1.2
-
- The first four sorts are of a class that takes time proportional
- to n ** 2 to sort n items, shell sort is in an intermediate class
- that takes time proportional to roughly n ** 1.25, and the other
- four are in the best case class that takes time proportional to n
- log n.
-
- Note that two of the very simple sorts (bubble and insertion) are
- excellent on lists that are already sorted or nearly so. Tree
- sort is worse than shell sort at n this low because of high
- overhead, but would eventually catch up at much larger n. The
- simple quick sort is notoriously bad on presorted lists--at n =
- 800 it fails due to stack overflow during recursive calls. The
- improved quick sort overcomes this problem and will be the
- fastest sort of this collection for all but very unlikely
- sequences of numbers. (In a few rare cases, heap sort will beat
- it, since the improved quicksort still has a bad worst case,
- while heap sort takes about the same time for best and worse case
- lists.)
-
-
-
-
-
-
- 1
-
-
-
-
-
-