note
hossman
The way i'm reading your post is: "I'm looking
for an algorithm that will let me sort elements 1-N,
given that the only memory available is
an array from 1-N, plus a single temp variable, and the
only allowed opperation is
<code> move(n, m) </code>".
<p>
This sounds alot like
<a href="http://www.ship.edu/~cawell/Sorting/bubintro.htm">Bubble Sort</a>
to me.
<p>
The distinction being that Bubble Sort is usually defined in terms
of a <code> swap(a, b) </code> method which is considered
atomic. Since <code> swap </code>) can be (and frequently is)
defined in terms of two moves that use a temp variable,
Bubble Sort can solve your problem given your constraints --
uut it will never come close to a solution with a minimal
number of moves.
It will only ever call <code> move(a, 0) </code> and
<code> move(0, a) </code>.
<p>
I can't think of any other constant space sorting
algorithms
(that operate on arrays). But one extremely important
question that needs to be answered before you can even
try to for finding an optimal algorithm is:
how do you define optimal? is <code>move(n,m)</code>
the only operation with a "cost" ? what about doing
comparispons or slots? Would a solution that analyzed all
of the slots in detail first,then built up a list of
moves be considered optimal?
<p>
(I ask, because you're post only refers to the ideal
situation being one in which "the number of <b>moves</b>
is minimal.")
144470
144470