Sort by insert - code problem

Please discuss general Delphi programming topics here.

Sort by insert - code problem

Postby Johnny_Bit » December 2nd, 2003, 9:01 pm

Code: Select all
procedure Sort(const AIn: array of Integer; var AOut: Array of Integer);
var
  I, J, K, Found: Integer;
begin
  for I:=0 to High(AIn) do
    AOut[I]:=-1;
  AOut[0]:=AIn[0];
  for I:=1 to High(AIn) do
    begin
      Found:=AIn[I];
      for J:=0 to High(AIn) do
        begin
          if AOut[J]>Found then
            begin
              for K:=High(AIn) downto J-1 do
                begin
                  if K-1>=0 then
                    AOut[K]:=AOut[K-1]
                end;
              AOut[J]:=Found;
              Break;
            end
          else
            if AOut[J]=-1 then
              AOut[J]:=Found;
        end;
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Size, I: Integer;
  Unsorted, Sorted: array of Integer;
  Contains: string;
begin
  Randomize;
  Size:=StrToInt(Edit1.Text);
  SetLength(Unsorted, Size);
  SetLength(Sorted, Size);
  Memo1.Lines.Add('Log: Begin');
  for I:=0 to High(Unsorted) do
    begin
      Unsorted[I]:=Random(255);
      Contains:=Contains+IntToStr(Unsorted[I])+', ';
    end;
  Memo1.Lines.Add(Contains);
  Memo1.Lines.Add('Log: Begin sort');
  Contains:='';
  Sort(Unsorted, Sorted);
  Memo1.Lines.Add('Log: End sort');
  for I:=0 to High(Sorted) do
    begin
      Contains:=Contains+IntToStr(Sorted[I])+', ';
    end;
  Memo1.Lines.Add(Contains);
  Memo1.Lines.Add('Log: End');
end;


Code compiles, program nicely executes, but sorting is simply bad... for example:

in AIn table are numbers: 88, 57, 214, 228, 83, 72, 250, 16, 205, 9
in AOut table, after sort are numbers: 9, 16, 57, 72, 83, 83, 205, 214, 214, 214

sort is working (as you can see items are sorted in model "a<b then a,b") but why bolded elements are doubled?
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Kambiz » December 2nd, 2003, 11:23 pm

I don't know the answer, however I have another algorithm to suggest. :D

Code: Select all
procedure QuickSort(var Values: array of Integer; LowBound, HighBound: Integer);
var
  L, R: Integer;
  Pivot, T: Integer;
begin
  repeat
    L := LowBound;
    R := HighBound;
    Pivot:= Values[(L + R) div 2];
    repeat
      while Values[L] < Pivot do
        Inc(L);
      while Values[R] > Pivot do
        Dec(R);
      if L <= R then
      begin
        T := Values[L];
        Values[L] := Values[R];
        Values[R] := T;
        Inc(L);
        Dec(R);
      end;
    until L > R;
    if LowBound < R then
      QuickSort(Values, LowBound, R);
    LowBound := L;
  until L >= HighBound;
end;

procedure Sort(var Values: array of Integer);
begin
  QuickSort(Values, Low(Values), High(Values));
end;
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: Sort by insert - code problem

Postby KiteK » December 3rd, 2003, 6:43 am

Hi! I wrote this code:

Code: Select all

procedure Sort(const AIn: array of Integer; var AOut: Array of Integer);

  function Times(C: Char; X: Integer): String;   //i didn't remember true function :)
  var
    LP: Integer;
  begin
  Result := '';
  for LP := 1 to X do
    Result := Result + C;
  end;

var
  LP, L: Integer;
  SL, SL2: TStringList;
  S: String;
begin
L := 0;
SL := TStringList.Create;
for LP := Low(AIn) to High(AIn) do
begin
  S := IntToStr(AIn[LP]);
  if (Length(S) > L) then
    L := Length(S);
  SL.Add(S);
end;
for LP := 0 to SL.Count - 1 do
begin
  S := SL.Strings[LP];
  if (L > Length(S)) then
  begin
    S := Times('0', L - Length(S)) + S;
  end;
  SL.Strings[LP] := S;
end;
SL.Sort;
SL2 := TStringList.Create;
for LP := 0 to SL.Count - 1 do
begin
  if (SL2.IndexOf(SL.Strings[LP]) = -1) then
    SL2.Add(SL.Strings[LP]);
end;
for LP := 0 to SL2.Count - 1 do
  AOut[LP] := StrToInt(Copy(SL2.Strings[LP], 1, Length(SL2.Strings[LP])));
SL.Free;
SL2.Free;

end;



I know, there is TStringList, but code is work. Greetings, KiteK.[/quote][/code]
KiteK
Active Member
Active Member
 
Posts: 6
Joined: November 27th, 2003, 4:05 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 16 guests

cron