PROGRAM SortArray;
{Programmer: Gregory A. Perkins
CS 65}
{Creates an Array with the data in any order - Sorts the data sequentially}
{Programmed and tested to be enplaced in a program at a later date as a procedure}
{Declarations Section}
CONST MaxElements = 100;
TYPE NumArray = ARRAY[1..MaxElements] OF INTEGER;
VAR Data : NumArray;
Elements, Number, Count, Replace, HighVal : INTEGER;
BEGIN
{Initialization Section}
Elements := 0; Number := 0; Count := 0;
Replace := 0; HighVal := 0;
{Get the number of elements in the array from user}
WRITE('Enter the number of elements (MAX = 100) in your Array : ');
READLN(Number);
{Read in the values of the user's array}
FOR Count := 1 TO Number DO
BEGIN
WRITE('Element #', Count:1, ' : ');
READLN(Data[Count]);
WRITELN;
END;
{Display the "ENTERED" array}
WRITELN('Here is the array that you entered...');
FOR Count := 1 TO Number DO
WRITE(Data[Count]:1, ' : ');
WRITELN;
WRITELN;
{Sort the array sequential from lowest value to highest value}
WRITELN('Now Sorting Array!!!');
{SORTER}
{*********************************************************************************}
Elements := Number; {used as a loop counter in the 'sorter'}
WHILE (Elements >= 2) DO
BEGIN {while statement}
Replace := 0; {reset to test for changes made}
HighVal := 0; {reset to ensure value integrity}
FOR Count := 1 TO Elements DO
BEGIN {loop}
IF (Data[Count] > HighVal) THEN
BEGIN {conditional to change values}
HighVal := Data[Count]; {set to value currently checking}
Replace := Count; {save position of element to replace}
END; { }
END; {of FOR loop}
{Ensure that a change has been made before moving elements}
IF (Replace > 0) THEN
BEGIN
Data[Replace] := Data[Elements];
Data[Elements] := HighVal;
END;
{Decrease elements counter to escape while loop}
Elements := Elements - 1; {decrease range to check within array}
END; {of WHILE loop}
{*********************************************************************************}
WRITELN('Here is your ''sorted'' array...');
FOR Count := 1 TO Number DO
WRITE(Data[Count]:1 , ' : ');
WRITELN;
END. {of program}