DELPHI AREA
MESSAGE BOARD
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sort by insert - code problem

 
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming
View previous topic :: View next topic  
Author Message
Johnny_Bit
Junior Member


Joined: 15 Jun 2003
Posts: 98

PostPosted: 02/12/03 21:01    Post subject: Sort by insert - code problem Reply with quote

Code:
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?
Back to top
View user's profile
Kambiz
Administrator


Joined: 07 Mar 2003
Posts: 250

PostPosted: 02/12/03 23:23    Post subject: Reply with quote

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

Code:
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;
Back to top
View user's profile Send e-mail Visit poster's website
KiteK
Member


Joined: 27 Nov 2003
Posts: 6

PostPosted: 03/12/03 06:43    Post subject: Re: Sort by insert - code problem Reply with quote

Hi! I wrote this code:

Code:


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]
Back to top
View user's profile Yahoo Messenger
Display posts from previous:   
   Reply to topic    DELPHI AREA Forum Index -> Delphi Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB 2.0.6 © 2001, 2002 phpBB Group