Now I recently saw this question (I can’t remember exactly where) about how few operations were needed to sort the list of numbers, excluding the reversal of the sublist.
Here is an example:
Unsorted input :3, 1, 2, 5, 4
One possible answer:
1. The inverse index from 0 to 3, giving 5, 2, 1, 3, 4
2. The inverse index from 0 to 4, giving 4, 3, 1, 2, 5
3. The inverse index from 0 to 3, giving 2, 1, 3, 4, 5
4. The inverse index from 0 to 1, giving1, 2, 3, 4, 5
However, having tried my hand on this problem, it turned out that for those who have not experienced the algorithms, it is really difficult to create a piece of code that finds the best solution. The answer noted above was simply accomplished by crudely forcing all possible combinations, but it becomes unbearably slow when lists are longer than 10 numbers (10 takes <2s, 14 took more than 10 minutes). Installing any existing sorting algorithms does not work either because they were created only to replace individual elements at a time (and replacing 2 numbers by reversing the sublist will take 1-2 operations that are not optimal). I also tried sorting the networks, because the maximum size was already determined before the program started, but I was also unlucky with them (they also rely on swap, which is inefficient,when you have the opportunity to change several at a time). I also tried to make a function that “optimized” the list of operations, like trying to make existing sorting algorithms viable, but these answers were much longer than the optimal ones (think about 16 vs. 6).
, , , . , , "", , - . , , - , , StackOverflow.
, 0 100 ( ). - - 3 < N < 15, , , , .