Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Slow sorting??Help

How can I sort this in a better faster way?
Can Delphi really do this faster?


SortingTime=3 min 26 sec

unit Unitsortingslowly;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
x1,x2,x3,x4:integer;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
j,i,a:integer;

begin
screen.Cursor:=crHourglass;

for a:=1 to 50000 do begin
numbers1[a]:=a+2;
numbers2[a]:=a+1;
numbers3[a]:=a+3;
numbers4[a]:=a;
end;

//sorting numbers
for j:=1 to 50000 do begin
for i:=1 to j do begin
if numbers1[j] > numbers1[i] then begin


x1:= numbers1[j];
numbers1[j] := numbers1[i];
numbers1[i]:=x1;


x2:= numbers2[j];
numbers2[j] := numbers2[i];
numbers2[i]:=x2;


x3:= numbers3[j];
numbers3[j] := numbers3[i];
numbers3[i]:=x3;


x4:= numbers4[j];
numbers4[j] := numbers4[i];
numbers4[i]:=x4;

end;// if
end;// i
end;// j

screen.Cursor:=crDefault;


for a:=1 to 50 do begin
listbox1.items.add (IntToStr(numbers1[a])+

' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
' numbers3 > '+IntToStr(numbers3[a])+
' numbers4 > '+IntToStr(numbers4[a]));

end;

end;


end.

Comments

  • zibadianzibadian Member Posts: 6,349
    Delphi has a sorting demo, which includes QuickSort. This is one of the fastest sorting routines available.

    : How can I sort this in a better faster way?
    : Can Delphi really do this faster?
    :
    :
    : SortingTime=3 min 26 sec
    :
    : unit Unitsortingslowly;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : Dialogs, StdCtrls;
    :
    : type
    : TForm1 = class(TForm)
    : ListBox1: TListBox;
    : Button1: TButton;
    : procedure Button1Click(Sender: TObject);
    : procedure FormCreate(Sender: TObject);
    : private
    : { Private declarations }
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    : numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    : x1,x2,x3,x4:integer;
    : implementation
    :
    : {$R *.dfm}
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : var
    : j,i,a:integer;
    :
    : begin
    : screen.Cursor:=crHourglass;
    :
    : for a:=1 to 50000 do begin
    : numbers1[a]:=a+2;
    : numbers2[a]:=a+1;
    : numbers3[a]:=a+3;
    : numbers4[a]:=a;
    : end;
    :
    : //sorting numbers
    : for j:=1 to 50000 do begin
    : for i:=1 to j do begin
    : if numbers1[j] > numbers1[i] then begin
    :
    :
    : x1:= numbers1[j];
    : numbers1[j] := numbers1[i];
    : numbers1[i]:=x1;
    :
    :
    : x2:= numbers2[j];
    : numbers2[j] := numbers2[i];
    : numbers2[i]:=x2;
    :
    :
    : x3:= numbers3[j];
    : numbers3[j] := numbers3[i];
    : numbers3[i]:=x3;
    :
    :
    : x4:= numbers4[j];
    : numbers4[j] := numbers4[i];
    : numbers4[i]:=x4;
    :
    : end;// if
    : end;// i
    : end;// j
    :
    : screen.Cursor:=crDefault;
    :
    :
    : for a:=1 to 50 do begin
    : listbox1.items.add (IntToStr(numbers1[a])+
    :
    : ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    : ' numbers3 > '+IntToStr(numbers3[a])+
    : ' numbers4 > '+IntToStr(numbers4[a]));
    :
    : end;
    :
    : end;
    :
    :
    : end.
    :
    :

  • robberyrobbery Member Posts: 22
    [b][red]This message was edited by robbery at 2002-9-27 8:19:44[/red][/b][hr]
    How do I change my sorting code to let quicksort do it?
    I can't figure it out?
    I remarked where the sorting code begins end ends with ***********


    : Delphi has a sorting demo, which includes QuickSort. This is one of the fastest sorting routines available.
    :
    : : How can I sort this in a better faster way?
    : : Can Delphi really do this faster?
    : :
    : :
    : : SortingTime=3 min 26 sec
    : :
    : : unit Unitsortingslowly;
    : :
    : : interface
    : :
    : : uses
    : : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : : Dialogs, StdCtrls;
    : :
    : : type
    : : TForm1 = class(TForm)
    : : ListBox1: TListBox;
    : : Button1: TButton;
    : : procedure Button1Click(Sender: TObject);
    : : procedure FormCreate(Sender: TObject);
    : : private
    : : { Private declarations }
    : : public
    : : { Public declarations }
    : : end;
    : :
    : : var
    : : Form1: TForm1;
    : : numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    : : x1,x2,x3,x4:integer;
    : : implementation
    : :
    : : {$R *.dfm}
    : :
    : : procedure TForm1.Button1Click(Sender: TObject);
    : : var
    : : j,i,a:integer;
    : :
    : : begin
    : : screen.Cursor:=crHourglass;
    : :
    : : for a:=1 to 50000 do begin
    : : numbers1[a]:=a+2;
    : : numbers2[a]:=a+1;
    : : numbers3[a]:=a+3;
    : : numbers4[a]:=a;
    : : end;
    : :

    //From here***************** My sorting code starts here
    : : //sorting numbers
    : : for j:=1 to 50000 do begin
    : : for i:=1 to j do begin
    : : if numbers1[j] > numbers1[i] then begin
    : :
    : :
    : : x1:= numbers1[j];
    : : numbers1[j] := numbers1[i];
    : : numbers1[i]:=x1;
    : :
    : :
    : : x2:= numbers2[j];
    : : numbers2[j] := numbers2[i];
    : : numbers2[i]:=x2;
    : :
    : :
    : : x3:= numbers3[j];
    : : numbers3[j] := numbers3[i];
    : : numbers3[i]:=x3;
    : :
    : :
    : : x4:= numbers4[j];
    : : numbers4[j] := numbers4[i];
    : : numbers4[i]:=x4;
    : :
    : : end;// if
    : : end;// i
    : : end;// j
    // To here ******************* my sorting code ends here
    : : screen.Cursor:=crDefault;
    : :
    : :
    : : for a:=1 to 50 do begin
    : : listbox1.items.add (IntToStr(numbers1[a])+
    : :
    : : ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    : : ' numbers3 > '+IntToStr(numbers3[a])+
    : : ' numbers4 > '+IntToStr(numbers4[a]));
    : :
    : : end;
    : :
    : : end;
    : :
    : :
    : : end.
    : :
    : :
    :
    :



  • zibadianzibadian Member Posts: 6,349
    You just paste the QuickSort() procedure into your program. Then you remove the array parameter from the head, and change all A references to your numbers1 array. Lastly you change the swap part by removing the call to VisualSwap, and making the appropriate changes to the three lines following it. Lastly you delete the "if terminated" line.
    You sort code needs to be changed to call the updated QuickSort() procedure.

    : [b][red]This message was edited by robbery at 2002-9-27 8:19:44[/red][/b][hr]
    : How do I change my sorting code to let quicksort do it?
    : I can't figure it out?
    : I remarked where the sorting code begins end ends with ***********
    :
    :
    : : Delphi has a sorting demo, which includes QuickSort. This is one of the fastest sorting routines available.
    : :
    : : : How can I sort this in a better faster way?
    : : : Can Delphi really do this faster?
    : : :
    : : :
    : : : SortingTime=3 min 26 sec
    : : :
    : : : unit Unitsortingslowly;
    : : :
    : : : interface
    : : :
    : : : uses
    : : : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : : : Dialogs, StdCtrls;
    : : :
    : : : type
    : : : TForm1 = class(TForm)
    : : : ListBox1: TListBox;
    : : : Button1: TButton;
    : : : procedure Button1Click(Sender: TObject);
    : : : procedure FormCreate(Sender: TObject);
    : : : private
    : : : { Private declarations }
    : : : public
    : : : { Public declarations }
    : : : end;
    : : :
    : : : var
    : : : Form1: TForm1;
    : : : numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    : : : x1,x2,x3,x4:integer;
    : : : implementation
    : : :
    : : : {$R *.dfm}
    : : :
    : : : procedure TForm1.Button1Click(Sender: TObject);
    : : : var
    : : : j,i,a:integer;
    : : :
    : : : begin
    : : : screen.Cursor:=crHourglass;
    : : :
    : : : for a:=1 to 50000 do begin
    : : : numbers1[a]:=a+2;
    : : : numbers2[a]:=a+1;
    : : : numbers3[a]:=a+3;
    : : : numbers4[a]:=a;
    : : : end;
    : : :
    :
    : //From here***************** My sorting code starts here
    : : : //sorting numbers
    : : : for j:=1 to 50000 do begin
    : : : for i:=1 to j do begin
    : : : if numbers1[j] > numbers1[i] then begin
    : : :
    : : :
    : : : x1:= numbers1[j];
    : : : numbers1[j] := numbers1[i];
    : : : numbers1[i]:=x1;
    : : :
    : : :
    : : : x2:= numbers2[j];
    : : : numbers2[j] := numbers2[i];
    : : : numbers2[i]:=x2;
    : : :
    : : :
    : : : x3:= numbers3[j];
    : : : numbers3[j] := numbers3[i];
    : : : numbers3[i]:=x3;
    : : :
    : : :
    : : : x4:= numbers4[j];
    : : : numbers4[j] := numbers4[i];
    : : : numbers4[i]:=x4;
    : : :
    : : : end;// if
    : : : end;// i
    : : : end;// j
    : // To here ******************* my sorting code ends here
    : : : screen.Cursor:=crDefault;
    : : :
    : : :
    : : : for a:=1 to 50 do begin
    : : : listbox1.items.add (IntToStr(numbers1[a])+
    : : :
    : : : ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    : : : ' numbers3 > '+IntToStr(numbers3[a])+
    : : : ' numbers4 > '+IntToStr(numbers4[a]));
    : : :
    : : : end;
    : : :
    : : : end;
    : : :
    : : :
    : : : end.
    : : :
    : : :
    : :
    : :
    :
    :
    :
    :

  • robberyrobbery Member Posts: 22
    Is there a change that you can show me in my code?I'm really a beginner in this?The sorting code I use is from my time as a basic developer and that I converted to Delphi with no succes.

    I'm also little shy with my English(I'm from Sweden).It's easier to understand the coding that understand the explaning in English. Thanks anyway.


    : You just paste the QuickSort() procedure into your program. Then you remove the array parameter from the head, and change all A references to your numbers1 array. Lastly you change the swap part by removing the call to VisualSwap, and making the appropriate changes to the three lines following it. Lastly you delete the "if terminated" line.
    : You sort code needs to be changed to call the updated QuickSort() procedure.
    :
    : : [b][red]This message was edited by robbery at 2002-9-27 8:19:44[/red][/b][hr]
    : : How do I change my sorting code to let quicksort do it?
    : : I can't figure it out?
    : : I remarked where the sorting code begins end ends with ***********
    : :
    : :
    : : : Delphi has a sorting demo, which includes QuickSort. This is one of the fastest sorting routines available.
    : : :
    : : : : How can I sort this in a better faster way?
    : : : : Can Delphi really do this faster?
    : : : :
    : : : :
    : : : : SortingTime=3 min 26 sec
    : : : :
    : : : : unit Unitsortingslowly;
    : : : :
    : : : : interface
    : : : :
    : : : : uses
    : : : : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : : : : Dialogs, StdCtrls;
    : : : :
    : : : : type
    : : : : TForm1 = class(TForm)
    : : : : ListBox1: TListBox;
    : : : : Button1: TButton;
    : : : : procedure Button1Click(Sender: TObject);
    : : : : procedure FormCreate(Sender: TObject);
    : : : : private
    : : : : { Private declarations }
    : : : : public
    : : : : { Public declarations }
    : : : : end;
    : : : :
    : : : : var
    : : : : Form1: TForm1;
    : : : : numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    : : : : x1,x2,x3,x4:integer;
    : : : : implementation
    : : : :
    : : : : {$R *.dfm}
    : : : :
    : : : : procedure TForm1.Button1Click(Sender: TObject);
    : : : : var
    : : : : j,i,a:integer;
    : : : :
    : : : : begin
    : : : : screen.Cursor:=crHourglass;
    : : : :
    : : : : for a:=1 to 50000 do begin
    : : : : numbers1[a]:=a+2;
    : : : : numbers2[a]:=a+1;
    : : : : numbers3[a]:=a+3;
    : : : : numbers4[a]:=a;
    : : : : end;
    : : : :
    : :
    : : //From here***************** My sorting code starts here
    : : : : //sorting numbers
    : : : : for j:=1 to 50000 do begin
    : : : : for i:=1 to j do begin
    : : : : if numbers1[j] > numbers1[i] then begin
    : : : :
    : : : :
    : : : : x1:= numbers1[j];
    : : : : numbers1[j] := numbers1[i];
    : : : : numbers1[i]:=x1;
    : : : :
    : : : :
    : : : : x2:= numbers2[j];
    : : : : numbers2[j] := numbers2[i];
    : : : : numbers2[i]:=x2;
    : : : :
    : : : :
    : : : : x3:= numbers3[j];
    : : : : numbers3[j] := numbers3[i];
    : : : : numbers3[i]:=x3;
    : : : :
    : : : :
    : : : : x4:= numbers4[j];
    : : : : numbers4[j] := numbers4[i];
    : : : : numbers4[i]:=x4;
    : : : :
    : : : : end;// if
    : : : : end;// i
    : : : : end;// j
    : : // To here ******************* my sorting code ends here
    : : : : screen.Cursor:=crDefault;
    : : : :
    : : : :
    : : : : for a:=1 to 50 do begin
    : : : : listbox1.items.add (IntToStr(numbers1[a])+
    : : : :
    : : : : ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    : : : : ' numbers3 > '+IntToStr(numbers3[a])+
    : : : : ' numbers4 > '+IntToStr(numbers4[a]));
    : : : :
    : : : : end;
    : : : :
    : : : : end;
    : : : :
    : : : :
    : : : : end.
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    : :
    : :
    :
    :

  • zibadianzibadian Member Posts: 6,349
    [code]
    procedure QuickSort(iLo, iHi: Integer);
    var
    Lo, Hi, Mid, T: Integer;
    begin
    Lo := iLo;
    Hi := iHi;
    Mid := Numbers1[(Lo + Hi) div 2];
    repeat
    while Numbers1[Lo] < Mid do Inc(Lo);
    while Numbers1[Hi] > Mid do Dec(Hi);
    if Lo <= Hi then
    begin // Start Swapping Numbers arrays
    T := Numbers1[Lo];
    A[Lo] := Numbers1[Hi];
    Numbers1[Hi] := T;

    T := Numbers2[Lo];
    A[Lo] := Numbers2[Hi];
    Numbers2[Hi] := T;

    Inc(Lo); // Finished Swapping arrays
    Dec(Hi);
    end;
    until Lo > Hi;
    if Hi > iLo then QuickSort(iLo, Hi);
    if Lo < iHi then QuickSort(Lo, iHi);
    end;
    [/code]
    I myself haven't gotten round to delve into the theory behind the quicksort, so I cannot precisely explain what is happening. All I can tell you that this code should sort your Numbers1 & Numbers2 arrays as your own code did. You need to add the Number3 and 4 to the code yourself, but that shouldn't be too difficult.
  • robberyrobbery Member Posts: 22
    Where should I put this code!Not in the button click code or?

    Can I put the whole code after the button code and call the procedure in buttonclick code?(My Code ex:)

    I must be stupid but I can't figure it out?

    I understand the code and how to sort number3 and number 4.

    I have to delete my sorting code I understand.Shall I copy Your code there instead?

    Should be nice if You could put it in my code so I can see where it works.
    Excuse for my shy English.

    Thanks for taking Your time.


    : [code]
    : procedure QuickSort(iLo, iHi: Integer);
    : var
    : Lo, Hi, Mid, T: Integer;
    : begin
    : Lo := iLo;
    : Hi := iHi;
    : Mid := Numbers1[(Lo + Hi) div 2];
    : repeat
    : while Numbers1[Lo] < Mid do Inc(Lo);
    : while Numbers1[Hi] > Mid do Dec(Hi);
    : if Lo <= Hi then
    : begin // Start Swapping Numbers arrays
    : T := Numbers1[Lo];
    : A[Lo] := Numbers1[Hi];
    : Numbers1[Hi] := T;
    :
    : T := Numbers2[Lo];
    : A[Lo] := Numbers2[Hi];
    : Numbers2[Hi] := T;
    :
    : Inc(Lo); // Finished Swapping arrays
    : Dec(Hi);
    : end;
    : until Lo > Hi;
    : if Hi > iLo then QuickSort(iLo, Hi);
    : if Lo < iHi then QuickSort(Lo, iHi);
    : end;
    : [/code]
    : I myself haven't gotten round to delve into the theory behind the quicksort, so I cannot precisely explain what is happening. All I can tell you that this code should sort your Numbers1 & Numbers2 arrays as your own code did. You need to add the Number3 and 4 to the code yourself, but that shouldn't be too difficult.
    :

  • zibadianzibadian Member Posts: 6,349
    Here is where you need to put the QuickSort() procedure, and also how to call it. You need to delete your sorting code, because this will replace it. You cannot replace your code with the QuickSort code, because it calls itself (last 2 lines of the procedure). Delphi cannot compile a procedure with a call to a routine it doesn't know, so ALWAYS place procedure declarations before the calling procedure.
    Note: the colons (:) in front of the lines is from the copy-paste.
    [code]
    unit Unitsortingslowly;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

    type
    TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;
    numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    x1,x2,x3,x4:integer;
    implementation

    {$R *.dfm}

    : : procedure QuickSort(iLo, iHi: Integer);
    : : var
    : : Lo, Hi, Mid, T: Integer;
    : : begin
    : : Lo := iLo;
    : : Hi := iHi;
    : : Mid := Numbers1[(Lo + Hi) div 2];
    : : repeat
    : : while Numbers1[Lo] < Mid do Inc(Lo);
    : : while Numbers1[Hi] > Mid do Dec(Hi);
    : : if Lo <= Hi then
    : : begin // Start Swapping Numbers arrays
    : : T := Numbers1[Lo];
    : : A[Lo] := Numbers1[Hi];
    : : Numbers1[Hi] := T;
    : :
    : : T := Numbers2[Lo];
    : : A[Lo] := Numbers2[Hi];
    : : Numbers2[Hi] := T;
    : :
    : : Inc(Lo); // Finished Swapping arrays
    : : Dec(Hi);
    : : end;
    : : until Lo > Hi;
    : : if Hi > iLo then QuickSort(iLo, Hi);
    : : if Lo < iHi then QuickSort(Lo, iHi);
    : : end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    screen.Cursor:=crHourglass;
    QuickSort(1, 50000);
    screen.Cursor:=crDefault;

    for a:=1 to 50 do begin
    listbox1.items.add (IntToStr(numbers1[a])+

    ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    ' numbers3 > '+IntToStr(numbers3[a])+
    ' numbers4 > '+IntToStr(numbers4[a]));

    end;

    end;


    end.
    [/code]
    : Where should I put this code!Not in the button click code or?
    :
    : Can I put the whole code after the button code and call the procedure in buttonclick code?(My Code ex:)
    :
    : I must be stupid but I can't figure it out?
    :
    : I understand the code and how to sort number3 and number 4.
    :
    : I have to delete my sorting code I understand.Shall I copy Your code there instead?
    :
    : Should be nice if You could put it in my code so I can see where it works.
    : Excuse for my shy English.
    :
    : Thanks for taking Your time.
    :
    :
    : : [code]
    : : procedure QuickSort(iLo, iHi: Integer);
    : : var
    : : Lo, Hi, Mid, T: Integer;
    : : begin
    : : Lo := iLo;
    : : Hi := iHi;
    : : Mid := Numbers1[(Lo + Hi) div 2];
    : : repeat
    : : while Numbers1[Lo] < Mid do Inc(Lo);
    : : while Numbers1[Hi] > Mid do Dec(Hi);
    : : if Lo <= Hi then
    : : begin // Start Swapping Numbers arrays
    : : T := Numbers1[Lo];
    : : A[Lo] := Numbers1[Hi];
    : : Numbers1[Hi] := T;
    : :
    : : T := Numbers2[Lo];
    : : A[Lo] := Numbers2[Hi];
    : : Numbers2[Hi] := T;
    : :
    : : Inc(Lo); // Finished Swapping arrays
    : : Dec(Hi);
    : : end;
    : : until Lo > Hi;
    : : if Hi > iLo then QuickSort(iLo, Hi);
    : : if Lo < iHi then QuickSort(Lo, iHi);
    : : end;
    : : [/code]
    : : I myself haven't gotten round to delve into the theory behind the quicksort, so I cannot precisely explain what is happening. All I can tell you that this code should sort your Numbers1 & Numbers2 arrays as your own code did. You need to add the Number3 and 4 to the code yourself, but that shouldn't be too difficult.
    : :
    :
    :

  • robberyrobbery Member Posts: 22
    [b][red]This message was edited by robbery at 2002-9-30 4:48:53[/red][/b][hr]
    Thanks for your time with this problem of mine.I had some idea that It should be like your ex: but I was not so good that You are to solve it.

    Thanks to You for solving it for me.I'm very happy know and I don't have to take the grogg anymore for the nervs(Joking).

    Have a nice day.
    Robbery





    : Here is where you need to put the QuickSort() procedure, and also how to call it. You need to delete your sorting code, because this will replace it. You cannot replace your code with the QuickSort code, because it calls itself (last 2 lines of the procedure). Delphi cannot compile a procedure with a call to a routine it doesn't know, so ALWAYS place procedure declarations before the calling procedure.
    : Note: the colons (:) in front of the lines is from the copy-paste.
    : [code]
    : unit Unitsortingslowly;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : Dialogs, StdCtrls;
    :
    : type
    : TForm1 = class(TForm)
    : ListBox1: TListBox;
    : Button1: TButton;
    : procedure Button1Click(Sender: TObject);
    : procedure FormCreate(Sender: TObject);
    : private
    : { Private declarations }
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    : numbers1,numbers2,numbers3,numbers4:array[1..50000]of integer;
    : x1,x2,x3,x4:integer;
    : implementation
    :
    : {$R *.dfm}
    :
    : : : procedure QuickSort(iLo, iHi: Integer);
    : : : var
    : : : Lo, Hi, Mid, T: Integer;
    : : : begin
    : : : Lo := iLo;
    : : : Hi := iHi;
    : : : Mid := Numbers1[(Lo + Hi) div 2];
    : : : repeat
    : : : while Numbers1[Lo] < Mid do Inc(Lo);
    : : : while Numbers1[Hi] > Mid do Dec(Hi);
    : : : if Lo <= Hi then
    : : : begin // Start Swapping Numbers arrays
    : : : T := Numbers1[Lo];
    : : : A[Lo] := Numbers1[Hi];
    : : : Numbers1[Hi] := T;
    : : :
    : : : T := Numbers2[Lo];
    : : : A[Lo] := Numbers2[Hi];
    : : : Numbers2[Hi] := T;
    : : :
    : : : Inc(Lo); // Finished Swapping arrays
    : : : Dec(Hi);
    : : : end;
    : : : until Lo > Hi;
    : : : if Hi > iLo then QuickSort(iLo, Hi);
    : : : if Lo < iHi then QuickSort(Lo, iHi);
    : : : end;
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : screen.Cursor:=crHourglass;
    : QuickSort(1, 50000);
    : screen.Cursor:=crDefault;
    :
    : for a:=1 to 50 do begin
    : listbox1.items.add (IntToStr(numbers1[a])+
    :
    : ' < numbers1 numbers2 > '+IntToStr(numbers2[a])+
    : ' numbers3 > '+IntToStr(numbers3[a])+
    : ' numbers4 > '+IntToStr(numbers4[a]));
    :
    : end;
    :
    : end;
    :
    :
    : end.
    : [/code]
    : : Where should I put this code!Not in the button click code or?
    : :
    : : Can I put the whole code after the button code and call the procedure in buttonclick code?(My Code ex:)
    : :
    : : I must be stupid but I can't figure it out?
    : :
    : : I understand the code and how to sort number3 and number 4.
    : :
    : : I have to delete my sorting code I understand.Shall I copy Your code there instead?
    : :
    : : Should be nice if You could put it in my code so I can see where it works.
    : : Excuse for my shy English.
    : :
    : : Thanks for taking Your time.
    : :
    : :
    : : : [code]
    : : : procedure QuickSort(iLo, iHi: Integer);
    : : : var
    : : : Lo, Hi, Mid, T: Integer;
    : : : begin
    : : : Lo := iLo;
    : : : Hi := iHi;
    : : : Mid := Numbers1[(Lo + Hi) div 2];
    : : : repeat
    : : : while Numbers1[Lo] < Mid do Inc(Lo);
    : : : while Numbers1[Hi] > Mid do Dec(Hi);
    : : : if Lo <= Hi then
    : : : begin // Start Swapping Numbers arrays
    : : : T := Numbers1[Lo];
    : : : A[Lo] := Numbers1[Hi];
    : : : Numbers1[Hi] := T;
    : : :
    : : : T := Numbers2[Lo];
    : : : A[Lo] := Numbers2[Hi];
    : : : Numbers2[Hi] := T;
    : : :
    : : : Inc(Lo); // Finished Swapping arrays
    : : : Dec(Hi);
    : : : end;
    : : : until Lo > Hi;
    : : : if Hi > iLo then QuickSort(iLo, Hi);
    : : : if Lo < iHi then QuickSort(Lo, iHi);
    : : : end;
    : : : [/code]
    : : : I myself haven't gotten round to delve into the theory behind the quicksort, so I cannot precisely explain what is happening. All I can tell you that this code should sort your Numbers1 & Numbers2 arrays as your own code did. You need to add the Number3 and 4 to the code yourself, but that shouldn't be too difficult.
    : : :
    : :
    : :
    :
    :



Sign In or Register to comment.