BubbleSort

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;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值