|
I
don't really care, do what you like, it was just another option, but I have
to say:
1)
Copying the code for a bubble sort on an array is not "re-inventing the
wheel", it is cut-paste implementing a time-honoured and simple sorting
algorithm that everyone does in first year university. So most people will
quickly grasp what is happening, not that they wouldn't any other way either.
All you had to do was cut-paste, then change a couple of little bits of the
algorithm.
2)
Bubble sort, quick sort, shell sort, they all sort. Quick sort faster, bubble
sort uses less memory and is simpler to understand. At the end of the day modern
computers have so much speed and memory who cares?
3)
Bubble sort for an array is appropriate when you want to sort an
array.
Of
course, you could use a stringlist, and do a dodgy typecast of a TObject
pointer to an integer which you use to index the array. But we want to gain
a sorted array, not a sorted reference list.
Or you
could first copy each item of the array into some sort of object or
structure which you then add to a list, sort the list, then copy each
individually item back into another array. And then tell me it's quicker. Hey,
maybe it is, but you'd have to test that, not tell me off the top of your
head.
But
yeah, do what works for you, I was just providing another
option.
-----Original Message----- From:
[EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Leigh Wanstead Sent: Thursday, May 27 2004 4:28
p.m. To: NZ Borland Developers Group - Delphi List Subject:
RE: [DUG] dynamic array items
I
have not followed the discussion closely.
I
just read bubble sort topic and thought why reinvent the
whole wheel.
TList already has a method Sort which use QuickSort. QuickSort is
better than bubble sort.
All
you need to do is just offer a method TListSortCompare to compare the item in
the list.
Delphi version 7
Regards
Leigh
[Leigh Wanstead] -----Original
Message----- From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]On Behalf Of Allan,
Samuel Sent: Thursday, May 27, 2004 1:51 PM To: NZ Borland
Developers Group - Delphi List Subject: RE: [DUG] dynamic array
items
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.
|