简体   繁体   English

冒泡排序中的交换次数

[英]Number of swaps in Bubble Sort

I have a version of bubble sort: 我有一个冒泡版本:

int i, j;  

for i from n downto 1 
{
    for j from 1 to i-1 
    { 
        if (A[j] > A[j+1])
            swap(A[j], A[j+1]) 
    } 
}

I want to calculate the expected number of swaps using the above version of bubble sort. 我想使用上面版本的冒泡排序来计算预期的掉期数量。 The method used by me is shown below : 我使用的方法如下所示:

// 0 based index

float ans = 0.0;

for ( int i = 0; i < n-1; i++ )
{
    for ( int j = i+1; j < n; j++ ) {

        ans += getprob( a[i], a[j]); // computes probability that a[i]>a[j].
    }
}

Am i going the correct way or am I missing something? 我是正确的方式还是我错过了什么?

The best way to get the answer is by running the bubble-sort algorithm itself and including a counter after the swap() call. 获得答案的最佳方法是运行冒泡排序算法本身并在swap()调用后包含一个计数器。 Your calculation function would (a) need almost as long as the sort itself (depending on the runtime of swap() vs. getprob()) and (b) miss the point that the order of the elements changes while sorting. 您的计算函数将(a)几乎与排序本身一样长(取决于swap()与getprob()的运行时间)和(b)在排序时忽略元素顺序发生变化的点。

Btw, the exact number of swap() calls depends on the data you need to sort - you have n*(n-1)/2 comparisions and any of them could result in a swap (on average, half of the time you need to swap the compared elements). 顺便说一句,swap()调用的确切数量取决于你需要排序的数据 - 你有n *(n-1)/ 2次比较,其中任何一个都可能导致交换(平均而言,你需要一半的时间)交换比较的元素)。

Maybe this helps. 也许这有帮助。 Basically this provides a framework to run bubble sorts on a set of simulation datasets and to calculate the swap probability. 基本上,这提供了一个框架,用于在一组模拟数据集上运行气泡排序并计算交换概率。

Let this probability = p Then to find the expected number of swap operations, you need to apply this on a real dataset. 设这个概率= p然后,要找到预期的交换操作数,您需要在真实数据集上应用它。 Let n be the size of this dataset. 设n为该数据集的大小。 Then expected number = swapProbability * n * n 然后预期数量= swapProbability * n * n

n*n comes because the bubble sort has n * n number of expected operations. n * n之所以出现是因为冒泡排序具有n * n个预期操作。

float computeSwapProbability()
{
    int aNumSwaps = 0
    int aTotalNumberOfOperations = 0

    For all simulation datasets
    {


        int i, j;  

        for i from n downto 1 

        { 

            for j from 1 to i-1 

            { 
                aTotalNumberOfOperations++

                if (A[j] > A[j+1]) 
                {
                    swap(A[j], A[j+1]) 
                    aNumSwaps++
                }

            } 

        }
    }

    return (float)aNumSwaps/aTotalNumberOfOperations;
}

The best way to count swap is to include counter variable inside the if condition for swap. 计算交换的最佳方法是在交换的if条件中包含计数器变量。

    int swapCount=0;

    for (i = 0; i < (length-1); ++i) {
      for (j = 0; j < (length-i-1); ++j) {
        if(array[j] > array[j+1]) {
          temp = array[j+1];
          array[j+1] = array[j];
          array[j] = temp;
          swapCount++;
        }
      }
    }

    printf("Swap count : %d" ,swapCount);

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

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