Solved

Merge sort problem

Posted on 2004-09-01
8
1,027 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Amir Azhdari
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
sorry, i do not download unknown exe-files,
could you post your code here and point there where you have the problem?
0
 
LVL 6

Author Comment

by:Amir Azhdari
Comment Utility
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
 
LVL 6

Author Comment

by:Amir Azhdari
Comment Utility
Dear Meikl,
the problem is that I don't know why the algorithm can't sort all the grid
0
 
LVL 4

Expert Comment

by:mottor
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:mottor
Comment Utility
I mean " I didn't go deep into...."

0
 
LVL 6

Author Comment

by:Amir Azhdari
Comment Utility
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
 
LVL 4

Accepted Solution

by:
mottor earned 400 total points
Comment Utility
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
 
LVL 6

Author Comment

by:Amir Azhdari
Comment Utility
mottor, :)
thank you very much , it worked for me too :)
i'm going to give you the points...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now