| 1 |
(***************************************************************************** |
| 2 |
* A simple bubble sort program. Reads integers, one per line, and prints * |
| 3 |
* them out in sorted order. Blows up if there are more than 49. * |
| 4 |
*****************************************************************************) |
| 5 |
PROGRAM Sort(input, output); |
| 6 |
CONST |
| 7 |
(* Max array size. *) |
| 8 |
MaxElts = 50; |
| 9 |
TYPE |
| 10 |
(* Type of the element array. *) |
| 11 |
IntArrType = ARRAY [1..MaxElts] OF Integer; |
| 12 |
|
| 13 |
VAR |
| 14 |
(* Indexes, exchange temp, array size. *) |
| 15 |
i, j, tmp, size: integer; |
| 16 |
|
| 17 |
(* Array of ints *) |
| 18 |
arr: IntArrType; |
| 19 |
|
| 20 |
(* Read in the integers. *) |
| 21 |
PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType); |
| 22 |
BEGIN |
| 23 |
size := 1; |
| 24 |
WHILE NOT eof DO BEGIN |
| 25 |
readln(a[size]); |
| 26 |
IF NOT eof THEN |
| 27 |
size := size + 1 |
| 28 |
END |
| 29 |
END; |
| 30 |
|
| 31 |
BEGIN |
| 32 |
(* Read *) |
| 33 |
ReadArr(size, arr); |
| 34 |
|
| 35 |
(* Sort using bubble sort. *) |
| 36 |
FOR i := size - 1 DOWNTO 1 DO |
| 37 |
FOR j := 1 TO i DO |
| 38 |
IF arr[j] > arr[j + 1] THEN BEGIN |
| 39 |
tmp := arr[j]; |
| 40 |
arr[j] := arr[j + 1]; |
| 41 |
arr[j + 1] := tmp; |
| 42 |
END; |
| 43 |
|
| 44 |
(* Print. *) |
| 45 |
FOR i := 1 TO size DO |
| 46 |
writeln(arr[i]) |
| 47 |
END. |
| 48 |
|