通过交换使用最小互换相邻的元素排序顺序顺序、最小、元素

2023-09-11 22:37:16 作者:ざ碎情漂移ド

我们有N个数字(1,2,3,4,...,N)的一个未排序的序列。我们可以通过排序以特定顺序交换相邻元件的整体序列。给定一个序列,我怎么计算的顺序进行排序所需的最小可能的互换。

We have an unsorted sequence of N numbers (1, 2, 3, 4, ... N). We can sort the whole sequence by swapping adjacent elements in a specific order. Given a sequence, how do I compute the minimum possible swaps required to sort the sequence.

作为一个例子,考虑该序列{4,2,5,3,1}

As an example, consider the sequence {4, 2, 5, 3, 1}.

要排序的最佳方法是使用7互换按以下顺序

The best way to sort this is using 7 swaps in the following order

交换3,1:{4,2,5,1,3} 交换5,1:{4,2,1,5,3} 交换4,2:{2,4,1,5,3} 交换4,1:{2,1,4,5,3} 交换2,1:{1,2,4,5,3} 交换5,3:{1,2,4,3,5} 交换3,4:{1,2,3,4,5}

一个贪婪算法并不能证明卓有成效。一个反例是易于施工。对于接近解决方案的下一个显而易见的选择是动态规划。

A greedy algorithm did not prove fruitful. A counterexample was easy to construct. The next obvious choice for approaching the solution was dynamic programming.

假设我们有一个未排序的序列:{A1,A2,...艾,A(I + 1),...,一}。我们知道的顺序进行排序所需的交换的最小数量{艾,A(I + 1),...,一}是闽[艾,A(I + 1),...,一}。问题是要找到闵[A(I-1),艾​​,...,一]。

Say we have an unsorted sequence: {A1, A2, ...Ai, A(i+1), ..., An}. We know the minimum number of swaps required to sort the sequence {Ai, A(i+1), ..., An} is Min[Ai, A(i+1), ..., An}. The problem is finding Min[A(i-1), Ai, ..., An].

嗯,突然出现在我的脑海中首先想到的是刚加放A(I-1)在正确的位置,在已经排序序列{艾,...,一}所需的步骤。这样工作的:在质询给出的例子已使用完全相同的方法得到解决。

Well, the first thought that popped into my head was to just add the number of steps required to put A(i-1) in the correct place in the already sorted sequence {Ai, ..., An}. This works: the example given in the question has been solved using the exact same method.

但我不能证明该解决方案的有效性。这是经常与我的情况。当我想我已经解决了这个问题,最好的,我能做的就是获得一个直观的证明它。我在高中时,没有正规的训练,algorithmcs这样。我这样做纯粹是出于兴趣。

But I was not able to prove the validity of this solution. That is often the case with me. When I think I've solved the problem, the best I can do is obtain an 'intuitive' proof for it. I'm in high school and have no formal training in algorithmcs as such. I do it purely out of interest.

是否有严格的数学表示法,这个问题可以被转换成并证明正式?该符号可以扩展到其他的问题?怎么样?我想AP preciate它,如果它可能是psented的形式融为一体prehensible到高中,学生$ P $。

Is there rigorous mathematical notation that this problem can be converted into and proved formally? Can this notation be extended to other problems? How? I would appreciate it if it could be presented in a form comprehensible to a high-school-student.

推荐答案

这是一个经典算法问题。的最小数目如果互换等于阵列中反转的数目。如果我们有索引i和索引j,使得我>一Ĵ和I< Ĵ那么这就是所谓的反转。让我们来证明这种说法!我需要在路上几个引理:

This is a classical algorithm problem. The minimum number if swaps is equal to the number of inversions in the array. If we have index i and index j such that ai > aj and i < j then this is called an inversion. Let's prove this statement! I will need a few lemmas on the way:

引理1:如果没有反转的两个相邻元素则数组排序。 证明:让我们假设没有两个相邻的元素形成反转。这意味着我&LT = A I + 1 对于所有的i在区间[0,N-1]。由于&LT; = 是传递的,这将意味着数组排序。

Lemma 1: If there is no inversion of two adjacent elements then the array is sorted. Proof: Let's assume that no two adjacent elements form an inversion. This means that ai <= ai+1 for all i in the interval [0, n-1]. As <= is transitive this will mean that the array is sorted.

引理2:两个相邻元件的单个交换将通过至多1 降低阵列中反转的总数 证明:当我们交换两个相邻元素的我和 I + 1 它们对所有的阵列中的其他元素相对位置保持不变。这是那名后 I + 1 的所有元素,他们仍然会后 I + 1 和一个之前的所有元素我 ,他们将仍然是一个之前我。这也意味着,如果一个我或 I + 1 形成反转与元素Ĵ那么,他们仍然会形成与反转交换后。为此,如果我们换一个我和 I + 1 我们只会影响这两个因素来形成反转。作为两个元件可以参与不超过一个倒置我们还证明了引理

Lemma 2: A single swap of two adjacent elements will reduce the total number of inversions in the array by at most 1. Proof: when we swap two adjacent elements ai and ai+1 their relative position with respect to all the other elements in the array will remain unchanged. That is for all elements that were after ai+1, they will still be after ai+1 and for all elements before ai, they will still be before the ai. This also means that if ai or ai+1 formed an inversion with an element aj then, they will still form an inversion with it after the swap. Therefor if we swap ai and ai+1 we will affect only inversions that these two elements used to form. As two elements may participate in no more than one inversion we have also proved the lemma.

引理3:我们需要以阵列,其中NI是反转的在阵列数排序执行至少NI相邻元件的互换 证明:在排序后的数组有没有倒置。还根据引理2,单个交换可以通过在最多一个减少倒位数。因此,我们需要至少是反转的数目来执行许多交换

Lemma 3: We need to perform at least NI swaps of adjacent elements in order to sort the array where NI is the number of inversions in the array Proof: In a sorted array there are no inversions. Also according to lemma 2, a single swap can reduce the number of inversions by at most one. Thus we need to perform at least as many swaps as is the number of inversions.

引理4:我们可以随时进行排序相邻的元素,在这里就像上面NI是数组中反转的数目的NI交换阵列 证明:如果我们假设在我们阵中还有没有反转两个相邻的元素,然后根据引理1,阵列将被排序,我们正在做。 否则有至少一对形成的反转相邻元件。我们可以交换它们,从而通过正好一次减少倒位的总数量。我们可以准确地继续执行此操作NI次。

Lemma 4: We can always sort the array performing NI swaps of adjacent elements, where just like above NI is the number of inversions in the array. Proof: If we assume that in our array there is no inversion of two adjacent elements, then according to lemma 1, the array will be sorted and we are done. Otherwise there is at least one pair of adjacent elements that form an inversion. We can swap them and thus reduce the total number of inversions by exactly once. We can continue performing this operation exactly NI times.

现在我已经从答案的开始证明我的发言。

Now I have proven my statement from the beginning of the answer.

唯一的问题左边是如何计算在给定阵列反转的数目。你可以做,使用轻微的合并排序的修改,你积累的倒在合并阶段。你可以看看这个答案关于如何实现的细节。该算法的总体复杂性是 O(N *的log(n))

The only question left is how to count the number of inversions in a given array. You can do that using a slight modification of merge sort where you accumulate the inversions in the merge phase. You can have a look at this answer for details on how to implement that. The overall complexity of the algorithm is O(n*log(n)).