TIA
That is a loaded question.
By fast. Fast in terms of time to write the routine.
or execute fast.
And only a few - why worry that much about speed. The overhead for
handling large numbers can make it slower on short list.
For a quick and dirty and only good for short list -- long list it takes
too long
Simply compare the first one with the next one. if larger swap.
repeat until none are swapped. Like I said. Fast to code and fast on
limited number of integers. Try this with 10,000 integers. - - too slow.
The number of passes will be equal to number of integer values. Worst
case if the last one belongs at the start.
a modification to this that would speed things up. If it is determined
that a swap is needed. check and see if the position after the one being
compared would be better location. Repeat until best location is found.
then shift values up or down as needed to insert the value. This would
move on the first pass the first value to the last position, if it was
the largest. For values that are in the middle. It would search until
it found a value that larger and insert it before that value. Of course
that location is most likely not right. But will start getting the
numbers in order and would be faster than the simple method.
but again the extra logic for short list could take more time. short
interms of 10 to 20 values.
an other way.
develop a min and max routine for the integer values.
Call the routine with the full list.
Returns the min value. Place it at the first and then delete it from
the list of integer values. Or mark it as used or taken.
Loop
again on short list the extra overhead might cause it to take longer.
and a far out crazy way. Convert the integer values to strings. I know
but follow on. Make sure that the leading zeros are present.
Then insert them into a Tstrnglist with sort = true. Or call the sort
method. Then read them back. Assending or desending order as you desire.
and convert back to integers.
or an even crazier way.
For short quantities of integer values and over not too large of range.
Setup an array from the min to max allowed values. Array [min .. max]
of boolean
loop through the integer values with Array[integer_value] := true;
then loop looking for True
For I := low(array) to High(Array) do
If array[I] = true then
begin
List[K] := I;
inc[K];
End;
I would consider the last two for integer values up to 10000 or so in
quantity. Or more for the last one depending upon the amount of memory.
The concept of this one is two passes and you have the integer values
sorted. One to fill the boolean array and one to ready it back. That
is about as fast as you can get.
actually the last one will work to very high quantities of integer
values. Only limitation is that if they values have a large range - -
say 100 values but the min can be 1 and the max 100,000,000 this would
be quite slow in that case but if the number of values is 10,000 and the
range was in the 100,000, this could be quite fast - - depends upon
the integer values ranges ratio to number of integer values.
So have fun.
Jim P.
Jim P.
It depends on how many values there are and what possible range they
have. If it's just a "few" like you say then just about any sort will
be sufficient. Generally a quicksort is the fastest overall. If
you're really concerned about performance then you should carefully
profile your application to make sure that you're concentrating on the
part of the code that will provide the best overall improvement.
--
-Mike (TeamB)
TList has a adaquete sort function. maybe not the alltime fastest but should be sufficient for small sorts.
And it requires almost no coding just a simple function check TList.Sort for details
Best regards,
Mario
--
"Mr. X" <n...@mail.here> wrote in message
news:4323...@newsgroups.borland.com...
If you use the TList approach, you can reverse the sort by changing the
compare:TListSortCompare function you pass it.
--
Kirk Halgren
"OLE's demands on system resources are so high that it is tempting to think
that it will never prove useful to a large number of users. However, the
same thing was said about GUIs, and now we are all using them-and they are
actually quite snappy if you have a fast 486 or Pentium."
-- Charlie Calvert, Delphi Unleashed © 1995, p797
http://www.ericksasse.com.br/?p=363
To ascendent sort, just switch var positions in compare function:
Result := Integer(Numero1) - Integer(Numero2);
--
Erick Sasse
Brazil
or simply read the list back starting from the end instead of the start.
Jim P.
"Mr. X" wrote:
Use a kbmMemory table, with the index on the integers.