Title: Message
|
This
is also good, but will require a little modification. I pulled it off some
website, most of the code is actually setup and reporting on how the sort is
going. It has the advantage of being stock first year stuff. I put colour around
the sort code.
The Bubble SortThe concept behind the bubble sort is simple.
Instead of trying to compare one record to the entire rest of the list, why not
just compare it to it's neighbor? If every record is equal to or less than
the next one, the entire list is sorted. The logic of the bubble sort is
this:
- Start Scan
- Compare each record to the next one
- Swap any that are out of order.
- Continue scanning until no more swaps are required.
The whole
thing can be written in 30 lines of code.
procedure TForm1.SortNumbers(Sender:TObject);
const
ArySize=20
var
ary:array[1..ArySize] of extended;
e:extended;
t:byte;
NoExchanges:boolean;
begin
//fill the array
randomize;
For t:=1 to ArySize do ary[t]:=random*100;
Memo1.Lines.Clear;
For t:=1 to ArySize do Memo1.Lines.Add(Format('%8.4f',[ary[t]]));
//tell the user we are sorting
Memo2.Lines.Clear;
Memo2.Lines.Add('Sorting...');
refresh; //force a repaint
---------- Start of Sort -----------------------------------------
//sort really begins here
Repeat
NoExchanges:=true;
For t:=1 to ArySize-1 do
begin
if Ary[t] > Ary[t+1] then
begin //we have to switch.
NoExchanges:=False; //We have to tell the sort we aren't done.
e := Ary[t]; //store one of the numbers in a temporary location.
Ary[t] := Ary[t+1]; //put the other in its place.
Ary[t+1] := e; //Put the copy of the first number in the second slot.
end;
end;
//if there has been even one switch, NoExchanges is false;
Until NoExchanges; --------------------------------- End of Sort --------------------------------------------------------------------------
//output the results
Memo2.Lines.Clear;
For t:=1 to ArySize do Memo2.Lines.Add(Format('%8.4f',[ary[t]]));
end;
I've thought of that but the writing to text file
can occur multiple times and I would rather not fiddle with that part of
the program. I really just want to get the items in the Files array in
the correct order the quicket possible way.
For those interested, this seems to work very
well. Files is a global array of FileRec. TempFiles is a local
array of FileRec.
for FileCnt := 0 to
High(Files) do FileList.AddObject(ExtractFilename(Files[FileCnt].Filename),TObject(FileCnt)); FileList.Sort; SetLength(TempFiles,Length(Files)); for
FileCnt := 0 to High(TempFiles) do TempFiles[FileCnt] :=
Files[Integer(FileList.Objects[FileCnt])]; CopyMemory(Files,TempFiles,Sizeof(FileRec)*FileCnt);
Cheers,
Ross.
----- Original Message -----
Sent: Thursday, May 27, 2004 10:48
AM
Subject: Re: [DUG] dynamic array
items
What about a TList - the TList containing
pointers to the already-allocated records and you just resort the pointers
and then write to file the records contained at the pointer location using
the TList order.
Regards Paul McKenzie Analyst Programmer SMSS
Ltd.
|
_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi