1 { Updated SORTING.SWG on May 26, 1995 }
4 >I've been programming for a couple years now, but there are certain things
5 >that you seldom just figure out on your own. One of them is the multitude
6 >of standard sorting techniques. I did learn these, however, in a class I
7 >took last year in Turbo Pascal. Let's see, Bubble Sort, Selection Sort,
8 >Quick Sort.. I think that's what they were called. Anyway, if anyone
9 >has the time and desire I'd appreciate a quick run-down of each and if
10 >possible some source for using them on a linked list. I remember most of
11 >the code to do them on arrays, but I forget which are the most efficient
12 >for each type of data.
14 Here is a program that I was given to demonstrate 8 different types of sorts.
15 I don't claim to know how they work, but it does shed some light on what the
16 best type probably is. BTW, it can be modified to allow for a random number
17 of sort elements (up to maxint div 10 I believe).
19 ALLSORT.PAS: Demonstration of various sorting methods.
20 Released to the public domain by Wayel A. Al-Wohaibi.
22 ALLSORT.PAS was written in Turbo Pascal 3.0 (but compatible with
23 TP6.0) while taking a pascal course in 1988. It is provided as is,
24 to demonstrate how sorting algorithms work. Sorry, no documentation
25 (didn't imagine it would be worth releasing) but bugs are included
28 ALLSORT simply shows you how elements are rearranged in each
29 iteration of each of the eight popular sorting methods.
32 program SORTINGMETHODS;
37 N = 14; (* NO. OF DATA TO BE SORTED *)
38 Digits = 3; (* DIGITAL SIZE OF THE DATA *)
39 Range = 1000; (* RANGE FOR THE RANDOM GENERATOR *)
42 ArrayType = array[1..N] of integer;
43 TwoDimension = array[0..9, 1..N] of integer; (* FOR RADIX SORT ONLY *)
49 (*--------------------------------------------------------------------*)
51 procedure GetSortMethod;
57 writeln(' 1 FOR SELECT SORT ');
58 writeln(' 2 FOR INSERT SORT ');
59 writeln(' 3 FOR BUBBLE SORT ');
60 writeln(' 4 FOR SHAKE SORT ');
61 writeln(' 5 FOR HEAP SORT ');
62 writeln(' 6 FOR QUICK SORT ');
63 writeln(' 7 FOR SHELL SORT ');
64 writeln(' 8 FOR RADIX SORT ');
65 writeln(' 9 TO EXIT ALLSORT ');
76 Data[I] := random(Range)
99 procedure Swap(var X, Y : integer);
108 (*-------------------------- R A D I X S O R T ---------------------*)
110 function Hash(Number, H : integer) : integer;
113 3 : Hash := Number mod 10;
114 2 : Hash := (Number mod 100) div 10;
115 1 : Hash := Number div 100
119 procedure CleanArray(var TwoD : TwoDimension);
128 procedure PlaceIt(var X : TwoDimension; Number, I : integer);
136 if (X[I, J] > 0) then
140 until (Empty) or (J = N);
144 procedure UnLoadIt(X : TwoDimension; var Passed : ArrayType);
154 if (X[I, J] > 0) then
156 Passed[K] := X[I, J];
162 procedure RadixSort(var Pass : ArrayType; N : integer);
170 for Digit := Digits downto 1 do
176 Key := Hash(Element, Digit);
177 PlaceIt(Temp, Element, Key)
179 UnLoadIt(Temp, Pass);
185 (*-------------------------- H E A P S O R T -----------------------*)
187 procedure ReHeapDown(var HEAPData : ArrayType; Root, Bottom : integer);
193 while (Root * 2 <= Bottom)
196 if (Root * 2 = Bottom) then
199 if (HEAPData[Root * 2] > HEAPData[Root * 2 + 1]) then
202 MaxChild := Root * 2 + 1;
203 if (HEAPData[Root] < HEAPData[MaxChild]) then
205 Swap(HEAPData[Root], HEAPData[MaxChild]);
213 procedure HeapSort(var Data : ArrayType; NUMElementS : integer);
217 for NodeIndex := (NUMElementS div 2) downto 1 do
218 ReHeapDown(Data, NodeIndex, NUMElementS);
219 for NodeIndex := NUMElementS downto 2 do
221 Swap(Data[1], Data[NodeIndex]);
222 ReHeapDown(Data, 1, NodeIndex - 1);
228 (*-------------------------- I N S E R T S O R T -------------------*)
230 procedure StrInsert(var X : ArrayType; N : integer);
257 (*-------------------------- S H E L L S O R T ---------------------*)
259 procedure ShellSort(var A : ArrayType; N : integer);
272 for J := 1 to (N - Jump) do
275 if (A[J] > A[I]) then
287 (*-------------------------- B U B B L E S O R T -------------------*)
289 procedure BubbleSort(var X : ArrayType; N : integer);
296 for J := N downto I do
297 if (X[J] < X[J - 1]) then
298 Swap(X[J - 1], X[J]);
304 (*-------------------------- S H A K E S O R T ---------------------*)
306 procedure ShakeSort(var X : ArrayType; N : integer);
317 for J := R downto L do
318 if (X[J] < X[J - 1]) then
320 Swap(X[J], X[J - 1]);
325 if (X[J] < X[J - 1]) then
327 Swap(X[J], X[J - 1]);
336 (*-------------------------- Q W I C K S O R T ---------------------*)
338 procedure Partition(var A : ArrayType; First, Last : integer);
344 V := A[(First + Last) div 2];
348 while (A[Right] < V) do
350 while (A[Left] > V) do
352 if (Right <= Left) then
354 Swap(A[Right], A[Left]);
361 if (First < Left) then
362 Partition(A, First, Left);
363 if (Right < Last) then
364 Partition(A, Right, Last)
367 procedure QuickSort(var List : ArrayType; N : integer);
374 if (First < Last) then
375 Partition(List, First, Last)
378 (*-------------------------- S E L E C T S O R T -------------------*)
380 procedure StrSelectSort(var X : ArrayType; N : integer);
387 for I := 1 to N - 1 do
391 for J := (I + 1) to N do
404 (*--------------------------------------------------------------------*)
409 1 : StrSelectSort(Data, N);
410 2 : StrInsert(Data, N);
411 3 : BubbleSort(Data, N);
412 4 : ShakeSort(Data, N);
413 5 : HeapSort(Data, N);
414 6 : QuickSort(Data, N);
415 7 : ShellSort(Data, N);
416 8 : RadixSort(Data, N);
422 (*-------------------------------------------------------------------*)
431 writeln('PRESS ENTER TO RETURN');