I've been stuck on the second part of the following question from Skiena (The Algorithm Design Manual, 3rd ed.) for the past 24 hours and it's driving me nuts! I'm struggling to come up with new ideas at this point, and would appreciate some tips/solutions.
Exercise 4-27 Suppose you are given a permutation p of the integers 1 to n, and seek to sort them to be in increasing order [1, . . . , n]. The only operation at your disposal is reverse(p,i,j), which reverses the elements of a subsequence (p_i, ..., p_j) in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort.
The first part appears fairly straightforward if we brute-force it: locate the position of the largest/smallest value and reverse the subsequence that will place it in the correct position. Repeat for the next largest/smallest value until the sequence is in the correct order. Clearly this requires n reversals at most.
But if I apply this brute-force approach to the second part of the question, it seems like the cost could be O(n^2) in the worst-case scenario. Anyone got any suggestions?
There is an algorithm for in-place merge sort that fits:
The merge takes O(N log N) time:
Since merge takes O(N log N) time, the whole sort takes O(N log2 N) time.