A Way For Learning

Bubble Sort

No comments
Bubble Sort :
which sorts an array of integers into increasing order. The net effect of each pass of the inner loop of statements (3)-(6) is to "bubble" the smallest element toward the front of the array.

procedure bubble ( var A: array [1..n] of integer );
{ bubble sorts array A into increasing order }
var i, j, temp: integer;
begin
(1) for i := 1 to n-1 do
(2) for j := n downto i+1 do
(3) if A[j-1] > A[j] then begin
{ swap A[j - 1] and A[j] }
(4) temp := A[j-1];
(5) A[j-1] := A[j];
(6) AI> [j] := temp
end
end; { bubble }


No comments :

Post a Comment