Merge sort problem

Hi Experts
can you take a look at this code and help me where i'm wrong please ?.
http://azhdari.europe.webmatrixhosting.net/merge.exe


i have a stringgrid include 5 Records :

   F.Name     L.NAme        TAX
---------------------------------
  johnjohn     willywilly      1  
  john           willy            7
  john           will              5
  john           will              2
  jo              w                200  


and I want to sort&(group by) the records via Merge Algorithm :
a thing like to this :


   F.Name     L.NAme        TAX
---------------------------------
  jo              w                 200  
  john          will               2
  john          will               5
  john          willy             7
  johnjohn    willywilly       1  


thank you very much
Amir
LVL 6
Amir AzhdariAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mottorConnect With a Mentor Commented:
I didn't speek about k
The problem was  > in the statement
By me sorting is OK.
Also be carefull that when comparing numbers as strings '200' < '5'
0
 
kretzschmarCommented:
sorry, i do not download unknown exe-files,
could you post your code here and point there where you have the problem?
0
 
Amir AzhdariAuthor Commented:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    sort: TButton;
    Button1: TButton;
    procedure sortClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


var myarray:array[1..3]of  integer;
var myarray2formerge,b:array[0..400000,0..18]of string;
    groupcounter:integer;

  groupflag:boolean;
function compare(i:integer;l:integer):boolean;
var n:integer;
var k:integer;


begin
result:=true;

    groupflag:=true;
if groupcounter>0 then
 begin
for k:=1 to groupcounter do
   if myarray2formerge[i,myarray[k]]<>myarray2formerge[l,myarray[k]] then
    begin
     groupflag:=false;
     break;
    end;
 end
 else
 k:=1;

if groupflag then
 begin
          if (k=1)and(myarray2formerge[i,myarray[groupcounter+1]]<myarray2formerge[l,myarray[groupcounter+1]])
            or
             (k<>1)and(myarray2formerge[i,myarray[groupcounter+1]]>myarray2formerge[l,myarray[groupcounter+1]])
           then
                     result:=false;
 end
   else
    result:=false;

end;

procedure fillarraytob(l:integer;i:integer);
var k:integer;
begin
            for k:=0 to form1.StringGrid1.ColCount-1 do
               b[i][k]:=myarray2formerge[l,k];
end;

procedure fillbtoarray(l:integer);
var k:integer;
begin
            for k:=0 to form1.StringGrid1.ColCount-1 do
               myarray2formerge[l,k]:=b[l][k];
end;

procedure MERGE(lowp:integer;midp:integer;highp:integer);
var h,i,k,j:integer;
begin
h:=lowp;
i:=lowp;
j:=midp+1;

while(h<=midp)and(j<=highp) do
begin
if (compare(h,j)) then
 begin
// ASLI     fillarraytob(h,i);
//   inc(h);

   fillarraytob(j,i);
   inc(j);

 end
else
 begin
// ASLI      fillarraytob(j,i);
//   inc(j);

   fillarraytob(h,i);
   inc(h);

 end;
 inc(i);
end; // end while

if (h>midp) then
 begin
 for k:=j to highp do
   begin
      fillarraytob(k,i);
    inc(i);
    end ;
  end
  else
 for k:=h to midp do
   begin
      fillarraytob(k,i);
    inc(i);
    end;

for k:=lowp to highp do
      fillbtoarray(k);

end;

procedure mergesort(lowp:integer;highp:integer);
var midp:integer;
begin

if lowp<highp then
  begin
    midp:=(lowp+highp)div 2;
    mergesort(lowp,midp);
    mergesort(midp+1,highp);
    MERGE(lowp,midp,highp);
  end;
end;

procedure Tform1.sortClick(Sender: TObject);
var i,j:integer;
var k,n:integer;
var mytemparray:string[200];
var l:longint;
    result1:boolean;

begin

// application.ProcessMessages;
 for i:=1 to 3 do myarray[i]:=-1;

 for i:=0 to 2 do
   begin
     myarray[i+1]:=i;
   end;



   // START MERGE ALGORITHM

   for i:=1 to form1.stringgrid1.rowcount-1 do
      for j:=0 to form1.stringgrid1.colcount-1 do
        myarray2formerge[i-1][j]:=form1.StringGrid1.Cells[j,i];

  for groupcounter:=0 to 2 do
       mergesort(0,form1.StringGrid1.RowCount-2);

  for i:=1 to form1.stringgrid1.rowcount-1 do
      for j:=0 to form1.stringgrid1.colcount-1 do
        form1.StringGrid1.Cells[j,i]:=myarray2formerge[i-1][j];
       
end;




procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
    s:string;
begin
with stringgrid1 do
begin

cells[0,1]:='johnjohn';
cells[1,1]:='willywilly';
cells[2,1]:='1';

cells[0,2]:='john';
cells[1,2]:='willy';
cells[2,2]:='7';


cells[0,3]:='john';
cells[1,3]:='will';
cells[2,3]:='5';

cells[0,4]:='john';
cells[1,4]:='will';
cells[2,4]:='2';

cells[0,5]:='jo';
cells[1,5]:='w';
cells[2,5]:='200';

end;

end;

end.










// DFM FILE

object Form1: TForm1
  Left = 396
  Top = 88
  Width = 381
  Height = 417
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 36
    Top = 28
    Width = 277
    Height = 233
    ColCount = 3
    FixedCols = 0
    RowCount = 6
    TabOrder = 0
    RowHeights = (
      24
      24
      24
      24
      24
      24)
  end
  object sort: TButton
    Left = 136
    Top = 336
    Width = 95
    Height = 25
    Caption = 'Merge Sort'
    TabOrder = 1
    OnClick = sortClick
  end
  object Button1: TButton
    Left = 145
    Top = 290
    Width = 75
    Height = 25
    Caption = 'Fill Grid'
    TabOrder = 2
    OnClick = Button1Click
  end
end
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Amir AzhdariAuthor Commented:
Dear Meikl,
the problem is that I don't know why the algorithm can't sort all the grid
0
 
mottorCommented:
I did go into the logic but look at this :

                                                                  May be should be <, not >
Original:
(k<>1)and(myarray2formerge[i,myarray[groupcounter+1]]>myarray2formerge[l,myarray[groupcounter+1]])
           then
                     result:=false;
Changed:
(k<>1)and(myarray2formerge[i,myarray[groupcounter+1]]<myarray2formerge[l,myarray[groupcounter+1]])
           then
                     result:=false;
0
 
mottorCommented:
I mean " I didn't go deep into...."

0
 
Amir AzhdariAuthor Commented:
mottor,
in this case k is useless value :

       if (k=1)and(myarray2formerge[i,myarray[groupcounter+1]]<myarray2formerge[l,myarray[groupcounter+1]])
            or
             (k<>1)and(myarray2formerge[i,myarray[groupcounter+1]]<myarray2formerge[l,myarray[groupcounter+1]])

so we can remove that from the code , it mean :
       if (myarray2formerge[i,myarray[groupcounter+1]]<myarray2formerge[l,myarray[groupcounter+1]])
....


let me test that!
0
 
Amir AzhdariAuthor Commented:
mottor, :)
thank you very much , it worked for me too :)
i'm going to give you the points...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.