Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Bubble sort

English · Français · Suomi

Bubble sort is a simple sorting algorithm.

Characteristics

Unit UBubbleSort

pascal
unit UBubbleSort;

interface

type
  // data type
  TItemBubbleSort=integer;

procedure BubbleSort( var a: array of TItemBubbleSort );

implementation

procedure swap( var a, b:TItemBubbleSort );
var
  temp : TItemBubbleSort;
begin
  temp := a;
  a := b;
  b := temp;
end;

procedure BubbleSort( var a: array of TItemBubbleSort );
var
  n, newn, i:integer;
begin
  n := high( a );
  repeat
    newn := 0;
    for i := 1 to n   do
      begin
        if a[ i - 1 ] > a[ i ] then
          begin
            swap( a[ i - 1 ], a[ i ]);
            newn := i ;
          end;
      end ;
    n := newn;
  until n = 0;
end;

end.

Example of usage

pascal
uses
  UBubbleSort
  ...

var
  a: array[0..100] of integer; 
begin
  ...
  BubbleSort(a);
  ...

See also


From the Free Pascal wiki, page "Bubble sort", as it stood on 2021-01-27 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .