【急求】给定数的序列只能相邻的两个数两两交换,最少需要多少次交换成为一个排列的序列,只需写出交换次
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【急求】给定数的序列只能相邻的两个数两两交换,最少需要多少次交换成为一个排列的序列,只需写出交换次相关的知识,希望对你有一定的参考价值。
Give you a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 1 4 3 2
swap (1 4) 4 1 3 2
swap (3 2) 4 1 2 3
swap (1 2) 4 2 1 3
swap (4 2) 2 4 1 3
swap (1 4) 2 1 4 3
swap (4 3) 2 1 3 4
swap (2 1) 1 2 3 4
So the sequence (1 4 3 2) can be sorted with seven swaps of adjacent numbers. However, it is even possible to sort it with such swaps:
Start with: 1 4 3 2
swap (4 3) 1 3 4 2
swap (4 2) 1 3 2 4
swap (3 2) 1 2 3 4
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?
我的作业已经做完了,ACCEPT了,但是我不理解我在用归并排序的时候,总是RUNTIME ERROR 同学说应该是realloc的时候出了问题,谁能给个具体解释
1432
1423
1243
1234
以上是关于【急求】给定数的序列只能相邻的两个数两两交换,最少需要多少次交换成为一个排列的序列,只需写出交换次的主要内容,如果未能解决你的问题,请参考以下文章