You have five coins, apparently alike, but actually of different weights. You also have a two arm scale.
Can you manage to sort the coins in ascending order, using the scale only seven times?
Bonus question: can it be done in fewer weighings?
(In reply to
re(3): Solution based on the O-Sort sorting algorithm by Charlie)
This conversation probably doesn't belong here, but here is the O-sort in Visual Basic anyway:
' O-Sort (Version 2)
Step = Int(Length / 2 - 1) ' Set initial step size
While (Step > 0)
For I = 1 To Length - Step ' Set the range of the lower search cells
If IntArray(I) > IntArray(I + Step) Then ' Compare cells
Temp = IntArray(I) ' \
IntArray(I) = IntArray(I + Step) ' | Swap Cells
IntArray(I + Step) = Temp ' /
End If
Next
Step = Int(Step * 0.86) ' Decrement the step size
Wend
In the tests I have done comparing it to the Shell sort, this algorithm is much faster. It's not quite as fast as the Quick sort for completely randomized data, but for data that is mostly sorted, the O-Sort is faster than the Quick sort.
Version 3 of the O-Sort is even faster, but it requires an additional few commands. I'll send you a link, if you like, to the data where I compare the three algorithms.
|
Posted by Erik
on 2004-06-03 09:37:02 |