[ Back to the overview Matrix ]

Test case : Bubble sort using Delphi 6

Lines used: 33
(The Delphi and Turbo Pascal codes are identical)
         
  [Create a new console application. In some versions of Delphi, you will need 
  to create a normal application, strip all forms, and add {$APPTYPE CONSOLE} in 
  the project source under "program"]
  
var                                           { declare variables }
  Numbers:array[0..24] of integer;            { 25 element array to hold the numbers}
  NumberCount:integer;                        { how many numbers in the array }
  s:string;                                   { buffer for user input }
  Changed:Boolean;                            { has something changed yes/no }
  At:Integer;                                 { what number are we checking }
  Temp:Integer;                               { temporary variable for swapping }

begin                                         { start main app }
  NumberCount:=0;                             { no numbers yet }
  readln(s);                                  { read the first line }
  while (s<>'') and (NumberCount<25) do       { while valid input and < 25 numbers }
  begin                                       { start loop block}
    Numbers[numbercount]:=StrToInt(s);        { convert input to number and store }
    inc(NumberCount);                         { we have one more number }
    readln(s);                                { read the next input }
  end;                                        { end loop block }

  Repeat                                      { start sort loop }
    Changed:=False;                           { nothing changed this loop }
    for At:=0 to NumberCount-2 do             { go from first to second last no}
      if Numbers[At]>Numbers[At+1] then       { current bigger than next ? }
        Begin                                 { start block swap }
          Temp:=Numbers[At];                  { store current number away }
          Numbers[At]:=Numbers[At+1];         { store next at current }
          Numbers[At+1]:=Temp;                { store backup in next }
          Changed:=True;                      { we changed something }
        end;                                  { end block swap }
  until not changed;                          { repeat until nothing changed }

  for At:=0 to NumberCount-1 do               { go through all numbers }
    Writeln(Numbers[at]);                     { print current number }
end.

Contributed by Andreas Koch, mail at kochandreas.com