?
Solved

Merge sort problem

Posted on 2004-09-01
8
Medium Priority
?
1,051 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 11951306
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
ID: 11951334
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
ID: 11951380
Dear Meikl,
the problem is that I don't know why the algorithm can't sort all the grid
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:mottor
ID: 11952671
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
 
LVL 4

Expert Comment

by:mottor
ID: 11952905
I mean " I didn't go deep into...."

0
 
LVL 6

Author Comment

by:Amir Azhdari
ID: 11954452
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 1600 total points
ID: 11955027
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
ID: 11955367
mottor, :)
thank you very much , it worked for me too :)
i'm going to give you the points...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 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