Re: Sorting question
From: Anonymous (nospam_at_noISP.com)
Date: 07/28/04
- Next message: Anonymous: "Re: High precision GCD"
- Previous message: K. Doniger: "Sorting question"
- In reply to: K. Doniger: "Sorting question"
- Next in thread: Matt: "Re: Sorting question"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 28 Jul 2004 03:51:47 GMT
K. Doniger wrote:
> I have to sort about 500 or fewer real numbers.
>
> I've looked at the pros and cons of the popular
> sorting methods: quick sort, heap sort, shell sort,
> and insertion sort. It would be easy to choose a
> method if my numbers were random, but they appear
> in sorted pairs
>
> a1, a2, b1, b2, ...
>
> where a1 < a2, b1 < b2, ...
>
> How can I best take advantage of the nonrandom
> nature of the data?
Use a merge sort, starting with runs (sequences already in order) of length 2. For a 2-way
merge, you compare a1 to b1 and output the smaller, leaving you with a run of 1 and a run
of 2. Compare the run of 1 to the first element in the run of 2 and output the smaller.
Repeat until you have a run of 4. Now move to the next 2 runs and repeat, ending with all
runs of 4. Then combine the first 2 runs of 4 in the same way. Eventually you finish with
a single run of n. For more details, see, e g, Knuth, Art of Computer Programming, vol 3.
sherNOwoodSPAM@computer.org (remove caps to get e-mail)
- Next message: Anonymous: "Re: High precision GCD"
- Previous message: K. Doniger: "Sorting question"
- In reply to: K. Doniger: "Sorting question"
- Next in thread: Matt: "Re: Sorting question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|