# Optimal sorting algorithm with modified cost

First, this was one of the four problems we had to solve in a project last year and I couldn’t find a suitable algorithm so we handle in a brute force solution.

Problem: The numbers are in a list that is not sorted and supports only one type of operation. The operation is defined as follows:

Given a position i and a position j the operation moves the number at position i to position j without altering the relative order of the other numbers. If i > j, the positions of the numbers between positions j and i - 1 increment by 1, otherwise if i < j the positions of the numbers between positions i+1 and j decreases by 1. This operation requires i steps to find a number to move and j steps to locate the position to which you want to move it. Then the number of steps required to move a number of position i to position j is i+j.

We need to design an algorithm that given a list of numbers, determine the optimal (in terms of cost) sequence of moves to rearrange the sequence.

Attempts: Part of our investigation was around NP-Completeness, we make it a decision problem and try to find a suitable transformation to any of the problems listed in Garey and Johnson’s book: Computers and Intractability with no results. There is also no direct reference (from our point of view) to this kind of variation in Donald E. Knuth’s book: The art of Computer Programing Vol. 3 Sorting and Searching. We also analyzed algorithms to sort linked lists but none of them gives a good idea to find de optimal sequence of movements.

Note that the idea is not to find an algorithm that orders the sequence, but one to tell me the optimal sequence of movements in terms of cost that organizes the sequence, you can make a copy and sort it to analyze the final position of the elements if you want, in fact we may assume that the list contains the numbers from 1 to n, so we know where we want to put each number, we are just concerned with minimizing the total cost of the steps.

We tested several greedy approaches but all of them failed, divide and conquer sorting algorithms can’t be used because they swap with no cost portions of the list and our dynamic programing approaches had to consider many cases.

The brute force recursive algorithm takes all the possible combinations of movements from i to j and then again all the possible moments of the rest of the element’s, at the end it returns the sequence with less total cost that sorted the list, as you can imagine the cost of this algorithm is brutal and makes it impracticable for more than 8 elements.

Our observations:

  n movements is not necessarily cheaper than n+1 movements (unlike swaps in arrays that are O(1)).

There are basically two ways of moving one element from position i to j: one is to move it directly and the other is to move other elements around i in a way that it reaches the position j.

At most you make n-1 movements (the untouched element reaches its position alone).

If it is the optimal sequence of movements then you didn’t move the same element twice.

-
It wouldn't surprise me at all to be wrong, but my intuition says to use insertion sort... I think it's $O(n^3)$ with the cost of the insertions. –  Isaac Jan 14 '11 at 7:02
If I understand the question, we may assume that the list contains the number $1$ to $n$. So we know where we want to put each number; we are just concerned with minimising the cost of the steps. Am I right? If so, the simple algorithm of placing the numbers in increasing order, from $1$ to $n$, takes at most $(n+1)+(n+2)+...+(n+n-1) = \frac{3}{2}n(n-1)$ steps. –  TonyK Jan 14 '11 at 9:56
How literally are we to take the statement "supports only one type of operation"? Does that mean we can't read values and have to design an oblivious algorithm? –  Charles Jan 14 '11 at 20:06
the idea is not to find an algorithm that orders the sequence, but one to tell me the optimal sequence of movements in terms of cost that organizes the sequence, u can make a copy and sort it to analize the final position of the elements if you want. –  David Darias Jan 14 '11 at 21:18
For example: 4-1-2-3 output pos: 0 to 3 cost: 3 this is better than pos: 1 to 0, 2 to 1, 3 to 2 cost: 9 –  David Darias Jan 14 '11 at 21:19