CSC 272 - Software II: Principles of Programming Languages

Dr. R. M. Siegfried

A Bubble Sort Program in Pascal

PROGRAM SortEm;
  CONST
   MaxArray = 10;       { Maximum array size }
  TYPE
    ArrayType = ARRAY[1..MaxArray] OF Integer;
  VAR
    Vector : ArrayType;
    NumElements, i : Integer;

  PROCEDURE ReadFile(VAR x : ArrayType; VAR n : Integer);
    {*********************************************}
    {*  Open a file specified by user for input  *}
    {*  Reads integers one to a line and then    *}
    {*  closes the file.  Note that parameters   *}
    {*  with the word VAR in front are passed    *}
    {*  by references - all others by value      *}
    {*********************************************}
   CONST
     FileNameLength = 30;
   TYPE
     FileNameType = String[FileNameLength];
    VAR
      FileName : FileNameType;
      InputFile : Text;
BEGIN
      { Get the file name and open it }
      Write('Enter file name ?');
      ReadLn(FileName);
      Assign(InputFile, FileName);
      Reset(InputFile);

      {****************************************}
      {*  Initially no values have been read  *}
      {*  Read increment the count and read   *}
      {*  one more and then display it on     *}
      {*  the screen                          *}
      {****************************************}
      n := 0;
      WHILE NOT Eof(InputFile) DO
        BEGIN
          n := n + 1;
          ReadLn(InputFile, x[n]);
          WriteLn('x(', n, ') = ', x[n])
        END;  { while not eof }
      Close(InputFile)  { Semicolon is a separator - not
                          needed before END }
    END;  { ReadFile }

  PROCEDURE BubbleSort(VAR x : ArrayType; n : Integer);
    { Classic Bubble Sort routine }
    VAR
      i, pass, Temp : Integer;
      Swap : Boolean;
    BEGIN
      { This is the first pass }
      pass := 1;
      { Keep sorting until in order }
      REPEAT
        {**************************************}
        {*  Outer loop governs the pass       *}
        {*  Swap is false until we encounter  *}
        {*  adjacent data out of order.       *}
        {*  Then toggle swap and rearrange    *}
        {*  the values                        *}
        {**************************************}
        Swap := False;
        FOR i := 1 TO n - pass DO
          BEGIN
            IF x[i] > x[i+1]
              THEN BEGIN
                     Swap := True;
                     Temp := x[i];
                     x[i] := x[i+1];
                     x[i+1] := Temp
                   END;  { then }
          END;  { for i.. }
      UNTIL NOT Swap;
    END;  { BubbleSort }

  {***********************************************}
  {*  Function header looks like this.  It can   *}
  {*  return any simple data type                *}
  {***********************************************}
  FUNCTION Maximum(x : ArrayType; n : Integer) : Integer;
    { Function to find the largest member of an array }
    VAR
      i : Integer;
      Largest : Integer;
    BEGIN
      Largest := -MaxInt;
      {********************************************}
      {*  BEGIN and END aren't needed because     *}
      {*  the FOR loop has only the IF statement  *}
      {*  and the IF has only the assignment      *}
      {********************************************}
      FOR i := 1 TO n
        DO IF x[i] > Largest THEN Largest := x[i];
      Maximum := Largest
    END;  { Maximum }

  { Main program is at the bottom }
  BEGIN { SortEm }
    ReadFile(Vector, NumElements);
    FOR i := 1 TO NumElements Do WriteLn(Vector[i]);
    WriteLn('The largest is ', Maximum(Vector, NumElements));
    BubbleSort(Vector, NumElements);
    FOR i := 1 TO NumElements Do WriteLn(Vector[i])
  END. { SortEm }

[Back To Pascal Program Index]