Home > Back-end >  Could you tell me how to sort Delphi two-dimensional array?
Could you tell me how to sort Delphi two-dimensional array?

Time:09-30

Type
Ta=record
I1: integer;
I2: integer;
end;

Var

A: array [0.. 10] of Ta.

//a in the data for the
0, 1258
2 102
1 99
0 2
.

On i2 to ascending i1 first, and then in ascending order

The result is: 0 2
0, 1258
1 99
2 102
.

Thank you very much

CodePudding user response:

Code, roughly as follows, you modify,

 
Procedure TForm1. Button1Click (Sender: TObject);
Var
I, j, N: integer;
The begin
//i1 Ascend
For I:=0 to Length (a) - 2 do
For j:=0 to Length (a) - 2 - I do
If a [j]. I1 & gt; + 1 a [j].journal of i1 then
The begin
N:=a [j]. I1;
A [j]. Journal of i1:=a [j + 1] i1.
+ 1 a [j].journal of i1:=N;
end;

//i2 Descend
For I:=0 to Length (a) - 2 do
For j:=0 to Length (a) - 2 - I do
If a [j]. Journal of i2 & lt; + 1 a [j].journal of i2 then
The begin
N:=a [j]. Journal of i2;
A [j]. Journal of i2:=a [j + 1] i2.
+ 1 a [j].journal of i2:=N;
end;
end;

CodePudding user response:

With bubble sort method

CodePudding user response:

To: feiba7288
The result should be wrong, you will become
0 2
0, 99
1 102
.

Silly way is carried out while the second sort segments, such as single row 0, 1 single row, more a cycle

CodePudding user response:

TO Andwo, I just told him that a basic sorting method, as TO how the actual effect, he can change the code under the flexible,

CodePudding user response:

To the building Lord, much experience under this algorithm, much practice To learn writing code is his own things,

CodePudding user response:

More practice can learn

CodePudding user response:

 
Procedure TForm1. Btn1Click (Sender: TObject);
Type
Ta=record
I1: integer;
I2: integer;
end;
Var
A: array [0.. 3] of Ta.
NTa, mTa: Ta;
I, j: integer;
StrTmp: string;
The begin
//initialize array content
NTa. I1:=0;
NTa. I2:=1258;
A [0] :=nTa;

NTa. I1:=2;
NTa. I2:=102;
A [1] :=nTa;

NTa. I1:=1;
NTa. I2:=99;
A [2] :=nTa;

NTa. I1:=0;
NTa. I2:=2;
A [3] :=nTa;

//an array to i1 ascending order
For I:=0 to high (a) do
The begin
NTa:=a [I];
For j:=I + 1 to high (a) do
The begin
The mTa:=a, [j].
If the mTa. I1 & lt; NTa. I1 then
The begin
A: [j]=nTa;
NTa:=the mTa;
end;
end;
A: [I]=nTa;
end;

//an array with i2 ascending order
For I:=0 to high (a) do
The begin
NTa:=a [I];
For j:=I + 1 to high (a) do
The begin
The mTa:=a, [j].
If the mTa. I2 & lt; NTa. I2 then
The begin
A: [j]=nTa;
NTa:=the mTa;
end;
end;
A: [I]=nTa;
end;

//print arrangement results
StrTmp:=';
For I:=0 to high (a) do
The begin
Showmessage (inttostr (I));
StrTmp:=strTmp + inttostr (a [I]. I1) + ', '+ inttostr (a [I] i2) + CRH (13);
end;
Showmessage (strTmp);
end;

CodePudding user response:

An array with i2 ascending order, determine the i1 is the same, the same should be replaced

CodePudding user response:

refer to the eighth floor zkroy36 response:
an array with i2 ascending order, of the same, whether the i1 should replace


Yes, should be mainly i1, second only to i2 sorting

CodePudding user response:

Two 32-bit integer into a 64 - bit int64, sorted again apart to
Lazy is two 32-bit integer into 20 string combination added to TStringList, sorted again apart to

St. The clear;
For I:=low to high (a) (a) do st. add (format (' %. 10 d %. 10 d '), the [[I] a i1, a [I] i2]));
St. sort;
For I:=low to high (a) (a) do the begin a [I]. I1:=strtoint (copy (st [I], 1, 10)); A [I]. I2:=strtoint (copy (st [I], 11, 20)); end;

CodePudding user response:

Insertion sort method

 
Procedure TForm1. Btn3Click (Sender: TObject);
Var
IntTmp: integer;
I, j, k: integer;
The begin
IntS:=Gettickcount;
For I:=0 to high do (x) - 1
The begin
J:=I + 1;
If x [j] <[I] then x
The begin
IntTmp:=x [j];
K:=I;
While ((x [k] & gt; IntTmp) and (k> Do=0))
The begin
X: [k + 1]=x [k].
K:=k - 1;
end;
X: [k + 1]=intTmp;
end;
end;


end;

CodePudding user response:



Before programming not familiar with.

When using the transfer two-dimensional one-dimensional.
Because in memory storage is continuous.
So directly use pointer. Then sort and one-dimensional sequence is the same

CodePudding user response:

When need the more the number of elements in the order, the efficiency of sorting method, the more obvious

Here are three kinds of sorting methods are the test results of

CodePudding user response:

Hill sorting method

 
Procedure ShellSort (var ABC: an array of Integer);
Var
I, j: Integer;
H: Integer;
Temp: Integer;
Ninth: Integer;
The begin
H:=1;
Ninth:=High (ABC) div 9;
While (h & lt;=Ninth) do
H:=(h * 3) + 1;
While (h & gt; 0) do
The begin
For I:=h to High (ABC) do
The begin
Temp:=ABC [I];
J:=I;
While (j & gt; + h=(0)) and (Temp & lt; ABC [j - h]) do
The begin
ABC: [j]=ABC [j - h];
Dec (j, h);
end;
ABC: [j]=Temp;
end;
H: h=div 3;
end;
end; nullnull
  • Related