Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Lazarus, FreePascal, Kylix, etc. (https://www.clubdelphi.com/foros/forumdisplay.php?f=14)
-   -   metodo de la burbuja (https://www.clubdelphi.com/foros/showthread.php?t=66588)

bothy 02-03-2010 08:42:32

metodo de la burbuja
 
Quisiera saber Como hago el metodo de la burbuja en delphi 2009 . . .
teniendo un listbox y un vector. . para ordenar numeros ya que sort los ordena alfabeticamente..

Neftali [Germán.Estévez] 02-03-2010 09:04:33

Para un array de enteros puedes usar algo así:

Código Delphi [-]
procedure TBubbleSort.Sort(var A: array of Integer);
var
  I, J, T: Integer;
begin
  for I := High(A) downto Low(A) do
    for J := Low(A) to High(A) - 1 do
      if A[J] > A[J + 1] then
      begin
        VisualSwap(A[J], A[J + 1], J, J + 1);
        T := A[J];
        A[J] := A[J + 1];
        A[J + 1] := T;
        if Terminated then Exit;
      end;
end;

Para un TStringList:

Código Delphi [-]
function BubbleSort( list: TStringList ): TStringList;
var
  i, j: Integer;
  temp: string;
begin
  // bubble sort
  for i := 0 to list.Count - 1 do begin
    for j := 0 to ( list.Count - 1 ) - i do begin
      // Condition to handle i=0 & j = 9. j+1 tries to access x[10] which
      // is not there in zero based array
      if ( j + 1 = list.Count ) then
        continue;
      if ( list.Strings[j] > list.Strings[j+1] ) then begin
        temp              := list.Strings[j];
        list.Strings[j]   := list.Strings[j+1];
        list.Strings[j+1] := temp;
      end; // endif
    end; // endwhile
  end; // endwhile
  Result := list;
end;

Si buscas en los foros, también encontrarás links muy interesantes (link, link, link)

Si deseas versiones alternativas, seguro que por internet encuentras muchas más.

bothy 03-03-2010 09:31:16

Buscaba algo mas facil pero con esta ayuda logre hacer mi metodo de la burbuja .. problema resuelto


La franja horaria es GMT +2. Ahora son las 10:40:36.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi