早教吧 育儿知识 作业答案 考试题库 百科 知识分享

急求给定数的序列只能相邻的两个数两两交换,最少需要多少次交换成为一个排列的序列,只需写出交换次GiveyouasequenceofNnumbers.Thegoalistomovethenumbersaroundsothatattheendthesequenceis

题目详情
【急求】给定数的序列只能相邻的两个数两两交换,最少需要多少次交换成为一个排列的序列,只需写出交换次
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的时候出了问题,谁能给个具体解释
▼优质解答
答案和解析
应该是3步吧
1432
1423
1243
1234
看了 急求给定数的序列只能相邻的两...的网友还看了以下: