|
|
DataMuseum.dkPresents historical artifacts from the history of: CP/M |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about CP/M Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - download
Length: 1664 (0x680)
Types: TextFile
Names: »QSORT.PAS«
└─⟦c96461903⟧ Bits:30002787 SW1602 COMPAS Pascal Version 3.07 Release 1.1
└─⟦this⟧ »QSORT.PAS«
PROGRAM QSORT; (*$K-,R-*)
(* This program demonstrates the quicksort algorithm, which *)
(* provides an extremely effecient method of sorting arrays in *)
(* memory. The program generates a list of 1000 random numbers *)
(* between 0 and 29999, and then sorts them using the QUICKSORT *)
(* procedure. Finally, the sorted list is output on the screen. *)
(* Note that stack and range checks are turned off (through the *)
(* compiler directive above) to optimize execution speed. *)
CONST
MAX = 1000;
TYPE
LIST = ARRAYÆ1..MAXÅ OF INTEGER;
VAR
DATA: LIST;
I: INTEGER;
(* QUICKSORT sorts elements in the array A with indices between *)
(* LO and HI (both inclusive). Note that the QUICKSORT proce- *)
(* dure provides only an "interface" to the program. The actual *)
(* processing takes place in the SORT procedure, which executes *)
(* itself recursively. *)
PROCEDURE QUICKSORT(VAR A: LIST; LO,HI: INTEGER);
PROCEDURE SORT(L,R: INTEGER);
VAR
I,J,X,Y: INTEGER;
BEGIN
I:=L; J:=R; X:=AÆ(L+R) DIV 2Å;
REPEAT
WHILE AÆIÅ<X DO I:=I+1;
WHILE X<AÆJÅ DO J:=J-1;
IF I<=J THEN
BEGIN
Y:=AÆIÅ; AÆIÅ:=AÆJÅ; AÆJÅ:=Y;
I:=I+1; J:=J-1;
END;
UNTIL I>J;
IF L<J THEN SORT(L,J);
IF I<R THEN SORT(I,R);
END;
BEGIN (*QUICKSORT*);
SORT(LO,HI);
END;
BEGIN (*QSORT*)
WRITE('Now generating 1000 random numbers...');
FOR I:=1 TO MAX DO DATAÆIÅ:=RANDOM(30000);
WRITELN;
WRITE('Now sorting random numbers...');
QUICKSORT(DATA,1,MAX);
WRITELN;
FOR I:=1 TO 1000 DO WRITE(DATAÆIÅ:8);
END.
«eof»