PROGRAM Ex14; {Quick Sort} uses crt; TYPE Sorted= ARRAY[1..100] OF integer; VAR X:Sorted; Count:integer; PROCEDURE ReadIt(VAR X:Sorted; VAR Count:integer); {Reads data into an array} BEGIN Count:=0; writeln('Type your data on one line'); WHILE NOT eoln DO BEGIN Count:= Count + 1; read(X[Count]) END {WHILE} END {ReadIt}; PROCEDURE Print(X:Sorted; Count:integer); {Print the array} VAR J:integer; BEGIN FOR J:= 1 TO Count DO write(X[J]: 4); writeln END {Print}; PROCEDURE Quick(Beginning, Ending:integer; VAR X:Sorted); VAR I, J, First:integer; {pre: array X is not sorted. post: array X is sorted} PROCEDURE Switch(M, N:integer); {Switches two elements of the array} VAR Temp:integer; BEGIN Temp:= X[N]; X[N]:= X[M]; X[M]:= Temp END {Switch}; BEGIN {I increases from the right of the array; J decreases from the left} I:= Beginning; J:= Ending; writeln('the range is from ', I, ' to ', J); if I < J then begin First:= X[I]; WHILE I < J DO BEGIN WHILE (X[I] <= First) AND (I < Ending) DO I:= I + 1; WHILE X[J] > First DO J:= J - 1; IF I < J THEN begin Switch(I, J); writeln('switching ', I, ' and ', J); print(x, count) end END {WHILE}; Switch(Beginning, J); writeln('switching beginnimg ', beginning, ' and ', J); print(x, count); readln; Quick(Beginning, J - 1, X); Quick(J + 1, Ending, X) end {if} END {Quick}; BEGIN {Main} clrscr; ReadIT(X, Count); writeln('The array read was'); Print(X, Count); Quick(1, Count, X); writeln('The sorted array is '); Print(X, Count) END.