如何找到比Ø更好的冒泡排序预期互换数(n ^ 2)时间 [英] How to find number of expected swaps in bubble sort in better than O(n^2) time

查看:156
本文介绍了如何找到比Ø更好的冒泡排序预期互换数(n ^ 2)时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我停留在问题 HTTP://www.$c$cchef.com / 7月12日/问题/ LEBOBBLE 这里就需要找到预期互换数。

I am stuck on problem http://www.codechef.com/JULY12/problems/LEBOBBLE Here it is required to find number of expected swaps.

我想为O(n ^ 2)解决方案,但它是超时。

I tried an O(n^2) solution but it is timing out.

在code是这样的:

swaps = 0
for(i = 0;i < n-1;i++)
    for(j = i+1;j<n;j++)
    {
        swaps += expected swap of A[i] and A[j]
    }

由于元件的概率变化,所以每对需要进行比较。所以,根据我上面的code段一定是最有效的,但它是超时。

Since probabilities of elements are varying, so every pair is needed to be compared. So according to me the above code snippet must be most efficient but it is timing out.

可不可以这样在O(nlogn)来完成,或者任何复杂的比为O(n ^ 2)。 给我任何暗示如果可能的话。

Can it be done in O(nlogn) or it any complexity better than O(n^2). Give me any hint if possible.

推荐答案

好吧,让我们想想这一点。

Alright, let's think about this.

我们知道,每一个数字需要进行交换,最终与每一个数字后,这是比它少,迟早的事。因此,互换对于给定数目的总数是数字的总数之后,这是比它少。然而,这仍然是为O(n ^ 2)的时间。

We realize that every number needs to be eventually swapped with every number after it that's less than it, sooner or later. Thus, the total number of swaps for a given number is the total number of numbers after it which are less than it. However, this is still O(n^2) time.

有关冒泡排序的外部循环的每次传球,一个元素被放置在正确的位置。不失一般性,我们将说,对于每一个通,最大元件其余被分类到该列表的末尾。

For every pass of the outer loop of bubble sort, one element gets put in the correct position. Without loss of generality, we'll say that for every pass, the largest element remaining gets sorted to the end of the list.

所以,在外部循环的第一遍,数量最多的是放置在末端。这需要的问:的掉期交易,其中的问:的是位置数数开始远离最终位置。

So, in the first pass of the outer loop, the largest number is put at the end. This takes q swaps, where q is the number of positions the number started away from the final position.

因此​​,我们可以说,这将需要的问:<子> 1 + Q 2 + ... + Q <子> N 交换来完成这个冒泡排序。但是,请记住,每交换,一个号码将被带到任何一个位置接近或一个位置远离他们的最终位置。在我们的具体情况下,如果一个数字是在更大数目的前面,并在或在其正确的位置前,人们更多的交换是必要的。但是,如果一个数字的背后是一个较大的数字背后是正确的位置上,少了一个交换是必需的。

Thus, we can say that it will take q1+q2+ ... +qn swaps to complete this bubble sort. However, keep in mind that with every swap, one number will be taken either one position closer or one position farther away from their final positions. In our specific case, if a number is in front of a larger number, and at or in front of its correct position, one more swap will be required. However, if a number is behind a larger number and behind it's correct position, one less swap will be required.

我们可以看到,这是真的与下面的例子:

We can see that this is true with the following example:

   5 3 1 2 4
=> 3 5 1 2 4
=> 3 1 5 2 4
=> 3 1 2 5 4
=> 3 1 2 4 5
=> 1 3 2 4 5
=> 1 2 3 4 5 (6 swaps total)

5移动4位。 3移动1格。 1移动2位。 2移动2位。 4移动1格。总计:10个空格

"5" moves 4 spaces. "3" moves 1 space. "1" moves 2 spaces. "2" moves 2 spaces. "4" moves 1 space. Total: 10 spaces.

需要注意的是3落后5,并在正确的位置前面。因此,人们更多的交换是必要的。 1和2是后面3和5 - 4更少互换将需要。 4落后5落后于它的正确位置,从而少了一个掉期是必要的。现在,我们可以看到,6的预期值相匹配的实际值。

Note that 3 is behind 5 and in front of its correct position. Thus one more swap will be needed. 1 and 2 are behind 3 and 5 -- four less swaps will be needed. 4 is behind 5 and behind its correct position, thus one less swap will be needed. We can see now that the expected value of 6 matches the actual value.

我们可以计算&Sigma公司; 问:的,首先分拣列表,保存在内存中的每个元素的原始位置,而这样做的那种。这是可能的 O(nlogn + N)的时间。

We can compute Σq by sorting the list first, keeping the original positions of each of the elements in memory while doing the sort. This is possible in O(nlogn + n) time.

我们还可以看到数字背后还有什么其他的数字,但这是不可能做到比为O(n ^ 2)时间快。但是,我们可以得到更快的解决方案。

We can also see what numbers are behind what other numbers, but this is impossible to do in faster than O(n^2) time. However, we can get a faster solution.

每一个交换有效地移动两个数字号码需要自己正确的位置,但有些掉期实际上什么也不做,因为一个最终被交换与每个数字得到closerafter它那不到它,而另一个得到farthersooner或更高版本。在我们的previous exampleThus的第一个交换,与3和5是在我们的例子中这个唯一的例子。

Every swap effectively moves two numbers number needs to their correct positions, but some swaps actually do nothing, because one be eventually swapped with every number gets closerafter it that's less than it, and another gets farthersooner or later. The first swap in our previous exampleThus, between "3" and "5" is the only example of this in our example.

我们要计算多少总数所说的掉期交易,有。这是作为一个练习的读者,但这里的最后一个提示:您只通过第一列表的一半必须循环。虽然这对于一个给定的数字是静止的,到底为O(n ^ 2),我们只有这样做为O(n ^ 2)就行了上半年总数的操作后,它使数字更快的整体。

We have to calculate how many total number of said swaps that there are. This is left as an exercise to the reader, but here's one last hint: you only have to loop through the first half of the list. Though this for a given number is still, in the end O(n^2), we only have to do O(n^2) operations on the first half total number of the list, making numbers after it much faster overall.

这篇关于如何找到比Ø更好的冒泡排序预期互换数(n ^ 2)时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆