http://qs321.pair.com?node_id=144516


in reply to sort with fewest moves

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  move(n, m)".

This sounds alot like Bubble Sort to me.

The distinction being that Bubble Sort is usually defined in terms of a  swap(a, b) method which is considered atomic. Since  swap) 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  move(a, 0) and  move(0, a).

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 move(n,m) 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?

(I ask, because you're post only refers to the ideal situation being one in which "the number of moves is minimal.")