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