University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bubblesort.pas 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. VAR
  13. {* Indexes, exchange temp, array size. *}
  14. i, j, tmp, size: integer;
  15. {* Array of ints *}
  16. arr: IntArrType;
  17. {* Read in the integers. *}
  18. PROCEDURE ReadArr(VAR size: Integer; VAR a: IntArrType);
  19. BEGIN
  20. size := 1;
  21. WHILE NOT eof DO BEGIN
  22. readln(a[size]);
  23. IF NOT eof THEN
  24. size := size + 1
  25. END
  26. END;
  27. BEGIN
  28. {* Read *}
  29. ReadArr(size, arr);
  30. {* Sort using bubble sort. *}
  31. FOR i := size - 1 DOWNTO 1 DO
  32. FOR j := 1 TO i DO
  33. IF arr[j] > arr[j + 1] THEN BEGIN
  34. tmp := arr[j];
  35. arr[j] := arr[j + 1];
  36. arr[j + 1] := tmp;
  37. END;
  38. {* Print. *}
  39. FOR i := 1 TO size DO
  40. writeln(arr[i])
  41. END.