I have a list of items stored in a remote database which may be unsorted, and I want to sort them. The database accepts commands of them form:
move item1 before item2
move item3 after item2
So, given a list of the form:
[1,3,2,7,6,0,4]
...how can I get the sequence of moves:
move 2 before 3
move 7 after 6
move 0 before 1
move 4 before 6
I assume a modification of the bubblesort algorithm would work, but I'm specifically looking for the most efficient implementation that is still pythonic, and that generates the fewest move commands.
UPDATE: the list is 1000-10000 long, and all items are unique - no repeats. Only a very small number of items - 1-10 - will be in the wrong place at any given time. Time is a concern - it should take seconds, not minutes - but it does not have to be extremely fast.
UPDATE 2: I would also like to move each item only once