简体   繁体   English

荷兰国旗的平均掉期次数

[英]average number of swaps in dutch national flag

I just want to know how can I get the average number of swaps in the two colors dutch national flag. 我只想知道如何获得两种颜色的荷兰国旗的平均掉期次数。 sorting positive and negative numbers instead of colors. 排序正负数字而不是颜色。 I'm assuming that the negative numbers are equal to the positive numbers and the array's numbers are given a random configuration, I'm not sure if my assumption is correct. 我假设负数等于正数,并且数组的数是随机配置的,我不确定我的假设是否正确。

Algorithm(A[0…n-1]):
    i ← 0
    j ← n - 1
    while i ≤ j:
        if A[i] < 0:            
           i ← i + 1
        else:
           swap(A[i], A[j])
           j ← j - 1

Thank you. 谢谢。

If the distribution of the positives and negatives is uniform, the first element is positive with probability 1/2 . 如果正负分布均匀,则第一个元素为正,概率为1/2 After the first iteration, the array is shortened by one element and the distribution of the subarray is still uniform (moving an element is a neutral operation). 第一次迭代后,该数组将缩短一个元素,并且子数组的分布仍然均匀(移动元素是中性操作)。

There are exactly n iterations before the subarray is empty thus the average number of swaps is n/2 . 在子数组为空之前恰好有n次迭代,因此平均交换次数为n/2 More precisely, the number of swaps follows a Binomial law with parameters 1/2 , n (this is a Bernouilli scheme). 更准确地说,掉期的数量遵循参数为1/2n的二项式定律(这是贝努利方案)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM