home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / pascal / sort.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-08-11  |  2.0 KB  |  58 lines

  1. { Bubble Sort Demonstration Program }
  2. {       Microsoft Pascal 3.1        }
  3.  
  4. { The main routine reads from the terminal an array  }
  5. { of ten real numbers and calls the procedure BUBBLE }
  6. { to sort them.                                      }
  7.  
  8. program BubbleSort(input,output);
  9.  
  10.   const
  11.     TABLEN = 10;                    { Length of reals table }
  12.  
  13.   type
  14.     TABLE = array[1 .. TABLEN] of real4;
  15.                                     { Table of reals type }
  16.   var
  17.     R:          TABLE;              { The table itself }
  18.     i:          integer;            { Table index }
  19.  
  20.   procedure Bubble(var t: TABLE);   { The sorting routine }
  21.     var
  22.       i:        integer;            { Index variable }
  23.       j:        integer;            { Index variable }
  24.       temp:     real4;              { Exchange variable }
  25.     begin
  26.       for i := 1 to 9 do            { Outer loop }
  27.         begin
  28.           for j := i + 1 to 10 do   { Inner loop }
  29.             begin
  30.               if (t[i] > t[j]) then { Sort in ascending order }
  31.                 begin
  32.                   temp := t[i];     { Perform the }
  33.                   t[i] := t[j];     { exchange of }
  34.                   t[j] := temp;     { table elememts }
  35.                 end;
  36.             end;
  37.         end;
  38.     end;
  39.  
  40.   begin
  41.     writeln(' Bubble Sort Demonstration Program.');
  42.     for i := 1 to 10 do             { Loop to read in reals }
  43.       begin
  44.         writeln(' Please input real number no. ',i:2);
  45.                                     { Prompt user }
  46.         readln(R[i]);               { Read response }
  47.       end;
  48.     Bubble(R);                      { Sort the array }
  49.     writeln;                        { Skip a line }
  50.     writeln(' The sorted ordering from lowest to highest is:');
  51.                                     { Print a header }
  52.     for i := 1 to 10 do             { Loop to print array }
  53.       begin
  54.         write(R[i]);                { Write a number }
  55.         if (i mod 5 = 0) then writeln;
  56.                                     { Five numbers per line }
  57.       end;
  58.   end.