Salut à tous, j'ai un petit problème avec un procédure de tri par insertion générique pour des tableaux (array)

La méthode fonctionne mais je dois la lancer plusieurs fois pour arriver à avoir mon tableau parfaitement trié :

Qu'est ce que j'aurais oublié ? , voyez vous une erreur ? Est-ce que je dois faire de la récursivité ? ou est-ce un comportement normal pour le tri par insertion ?

Voici le code pour un tri sur des integer

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function CompareInteger(Const elem1, elem2) : Integer;
var
  i1 : integer absolute elem1;
  i2 : integer absolute elem2;
begin
  if i1 = i2 then Result:=0
  else if i1 < i2 then Result:=-1
  else Result:=1;
end;
 
procedure TBZBaseArray.AnyInsertionSort(idxL, idxH, Dir : Integer; Stride : Integer; CompareFunc : TBZArraySortCompareFunc);
var
  ps, cs,ls,hs : Integer;
  li,hi : Integer;
  pb, SwapBuf : pByte;
begin
  pb:= PByte(FData);
 
  li :=idxL;
  hi :=idxH;
 
  ls := (li + 1) * Stride;
  hs := hi * Stride;
 
  SwapBuf := nil;
  GetMem(SwapBuf, Sizeof(T));  // T car c'est une classe générique
 
  Repeat
    Move(pb[ls], SwapBuf^, Stride);
    ps := ls;
    cs := ps;
    Dec(cs, Stride);
 
    If Dir >= 0 then
    begin
      While (ps > 0) and  (CompareFunc(pb[ps], pb[cs]) < 0) do
      begin
        Move(pb[cs], pb[ls] , Stride);
        dec(ps,stride);
        dec(cs,stride);
      end;
    end
    else
    begin
      While (ps > 0) and  (CompareFunc(pb[ps], pb[cs]) > 0) do
      begin
         Move(pb[cs], pb[ls] , Stride);
         dec(ps,stride);
         dec(cs,stride);
       end;
    end;
 
    if ps<>ls then Move(SwapBuf^, pb[ps], Stride);
    inc(ls, Stride);
 
  until ls > hs;
 
  FreeMem(Swapbuf);
  SwapBuf := nil;
end;
 
procedure TBZBaseArray.InsertionSort(Const Direction : byte; CompareFuncProc : TBZArraySortCompareFunc);
begin
  AnyInsertionSort(0, FCount-1, Direction, SizeOf(T), CompareFuncProc);
end;

Merci d'avance

A+

Jérôme