Procedure BubbleSort(
Var Data : IntArray;
Count : integer;
First : integer;
Last : integer;
Acend : boolean;
Var Succ : integer );
var
i,
temp,
s_begin,
s_end,
numsort : integer;
sorted : boolean;
Begin
{ initialize for full array sort }
s_begin := STARTINTARRAY;
s_end := STARTINTARRAY + count - 1 ;
numsort := Count;
Succ := 0; { assume success }
{ check for a subset sort; check parameters for correctness }
if (Count = 0) then
Begin
If (First < STARTINTARRAY) then
Begin { error: sort start index too low }
Succ := 1;
Exit;
End;
If (Last > MAXINTARRAY) then
Begin { error: sort end index too high }
Succ := 2;
Exit;
End;
if (Last < STARTINTARRAY) then
Begin { error: sort end index too low }
Succ := 3;
Exit;
End;
s_begin := First;
s_end := Last;
numsort := Last - First + 1;
End;
If numsort <= 1 then Exit; { only one element, so exit }
If Acend then
Begin { do the ascending sort }
Repeat
sorted := true; { flag default is true }
For i := s_begin to s_end -1 do
if (Data[i] < Data[i+1]) then
Begin
{ swap contents of Data[i] and Data[i+1] }
temp := Data[i];
Data[i] := Data[i+1];
Data[i+1] := temp;
{ set flag to indicate a swap occured; i.e., sort may not be completed }
sorted := false;
End;
Until sorted;
End Else
Begin { do the descending sort }
Repeat
sorted := true; { flag default is true }
For i := s_begin to s_end -1 do
if (Data[i] < Data[i+1]) then
Begin
{ swap contents of Data[i] and Data[i+1] }
temp := Data[i];
Data[i] := Data[i+1];
Data[i+1] := temp;
{ set flag to indicate a swap occured; i.e., sort may not be completed }
sorted := false;
End;
Until sorted;
End;
End;
//delphi/4544