Re: Sorting algorithm when comparison is heavy
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Fri, 11 Apr 2008 17:25:26 +0100
thomas.mertes@xxxxxx writes:
On 10 Apr., 06:35, Juha Nieminen <nos...@xxxxxxxxxxxxxx> wrote:<snip>
1) Assume that comparing elements is an extremely heavy operation,
while copying/swapping them is very light. What would be the most
optimal way of sorting such elements (in other words, a sorting
algorithms which uses the absolute minimum of comparisons)?
The first idea that I have:
I would consider some good sort algorithm like quicksort together
with some caching for comparisons. If the elements are referred
with a pointer I would use a hash map with two pointers as key.
What remains open is: Does a sorting algorithm like quicksort
really profit from such a caching.
It would surprise me if it did in general. The are O(n^2) possible
pairs, so even an algorithm that is O(n^2) in comparisons might never
compare the same pair twice. The exact number of pairs is n(n-1)/2
and this is the maximum done by insertion sort (I think). Bubble sort
does more, but who'd use that in the situation?
I suspect that all "good" sorts (those that are O(n.log(n)) will not
duplicate comparisons. I can't see how they could be O(n.log(n))
unless they avoided doing so.
You suggest using pointers, so I think the above applies. Of course,
you could have suggested using the values themselves. In that case,
if there are lots duplicate values, then you could win by caching the
results.
[In the OP's example (players in a tournament) all the values --
people -- are distinct, so there would be no gain there.]
--
Ben.
.
- Follow-Ups:
- Re: Sorting algorithm when comparison is heavy
- From: robertwessel2@xxxxxxxxx
- Re: Sorting algorithm when comparison is heavy
- References:
- Sorting algorithm when comparison is heavy
- From: Juha Nieminen
- Re: Sorting algorithm when comparison is heavy
- From: thomas . mertes
- Sorting algorithm when comparison is heavy
- Prev by Date: Re: C++ more efficient than C?
- Next by Date: Re: C++ more efficient than C?
- Previous by thread: Re: Sorting algorithm when comparison is heavy
- Next by thread: Re: Sorting algorithm when comparison is heavy
- Index(es):
Relevant Pages
|