[Delphi (Object Pascal)] pascal冒泡法排序代码 →→→→→进入此内容的聊天室

来自 , 2019-06-07, 写在 Delphi (Object Pascal), 查看 114 次.
URL http://www.code666.cn/view/547b85f3
  1. Procedure BubbleSort(  
  2.                        Var Data : IntArray;  
  3.                        Count    : integer;  
  4.                        First    : integer;  
  5.                        Last     : integer;  
  6.                        Acend    : boolean;  
  7.                        Var Succ : integer );  
  8.    
  9.  var  
  10.    i,  
  11.    temp,  
  12.    s_begin,  
  13.    s_end,  
  14.    numsort : integer;  
  15.    sorted  : boolean;  
  16.    
  17.  Begin  
  18.    { initialize for full array sort }  
  19.    s_begin := STARTINTARRAY;  
  20.    s_end   := STARTINTARRAY + count - 1 ;  
  21.    numsort := Count;  
  22.    Succ    := 0;    { assume success }  
  23.    
  24.    { check for a subset sort; check parameters for correctness }  
  25.    if (Count = 0) then  
  26.      Begin  
  27.        If (First < STARTINTARRAY) then  
  28.        Begin { error: sort start index too low }  
  29.          Succ := 1;  
  30.          Exit;  
  31.        End;  
  32.        If (Last > MAXINTARRAY) then  
  33.        Begin { error: sort end index too high }  
  34.          Succ := 2;  
  35.          Exit;  
  36.        End;  
  37.        if (Last < STARTINTARRAY) then  
  38.        Begin { error: sort end index too low }  
  39.          Succ := 3;  
  40.          Exit;  
  41.        End;  
  42.        s_begin := First;  
  43.        s_end   := Last;  
  44.        numsort := Last - First + 1;  
  45.      End;  
  46.    If numsort <= 1 then Exit; { only one element, so exit }  
  47.    
  48.    If Acend then  
  49.    Begin { do the ascending sort }  
  50.      Repeat  
  51.        sorted := true; { flag default is true }  
  52.        For i := s_begin to s_end -1 do  
  53.          if (Data[i] < Data[i+1]) then  
  54.          Begin  
  55.            { swap contents of Data[i] and Data[i+1] }  
  56.            temp      := Data[i];  
  57.            Data[i]   := Data[i+1];  
  58.            Data[i+1] := temp;  
  59.            { set flag to indicate a swap occured; i.e., sort may not be completed }  
  60.            sorted := false;  
  61.          End;  
  62.      Until sorted;  
  63.    End Else  
  64.    Begin { do the descending sort }  
  65.      Repeat  
  66.        sorted := true; { flag default is true }  
  67.        For i := s_begin to s_end -1 do  
  68.          if (Data[i] < Data[i+1]) then  
  69.          Begin  
  70.            { swap contents of Data[i] and Data[i+1] }  
  71.            temp      := Data[i];  
  72.            Data[i]   := Data[i+1];  
  73.            Data[i+1] := temp;  
  74.            { set flag to indicate a swap occured; i.e., sort may not be completed }  
  75.            sorted := false;  
  76.          End;  
  77.      Until sorted;  
  78.    End;  
  79.  End;  
  80.  
  81.  
  82.  
  83. //delphi/4544

回复 "pascal冒泡法排序代码"

这儿你可以回复上面这条便签

captcha