Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sort integers in list

126 views
Skip to first unread message

Mr. X

unread,
Sep 9, 2005, 1:34:19 AM9/9/05
to
Hi
I have a few integer values in a list and I would like to sort them in
ascending order ..
what is the fastest way to do this ?

TIA


Jim P

unread,
Sep 10, 2005, 5:12:14 PM9/10/05
to
Mr. X wrote:

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.

Mike Williams (TeamB)

unread,
Sep 10, 2005, 5:10:46 PM9/10/05
to
Mr. X wrote:

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)

Mario van Zeist

unread,
Sep 10, 2005, 5:33:22 PM9/10/05
to
Mr. X wrote:

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

unread,
Sep 11, 2005, 1:19:55 AM9/11/05
to
Thanks for the ideas all
Gives me a headstart now


"Mr. X" <n...@mail.here> wrote in message
news:4323...@newsgroups.borland.com...

Kirk Halgren

unread,
Sep 13, 2005, 9:34:29 AM9/13/05
to
"Mr. X" <n...@mail.here> wrote in message
news:4323...@newsgroups.borland.com...
> Thanks for the ideas all
> Gives me a headstart now

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


Erick Sasse

unread,
Sep 13, 2005, 10:50:16 PM9/13/05
to
If you are using TList to store ints, here is a sample with descendent
sort:

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

Jim P

unread,
Sep 14, 2005, 1:13:02 PM9/14/05
to
Erick Sasse wrote:

or simply read the list back starting from the end instead of the start.

Jim P.

Sanford Aranoff

unread,
Nov 13, 2005, 9:51:08 AM11/13/05
to

"Mr. X" wrote:

Use a kbmMemory table, with the index on the integers.


0 new messages