【问题标题】:Best case , Worst case , Random data input for insertion sort algorithm?最佳情况,最坏情况,插入排序算法的随机数据输入?
【发布时间】:2011-11-08 14:19:55
【问题描述】:

这是我最初输入随机数然后使用插入排序方法对其进行排序的代码。

        #include<iostream>

        #include <cstdlib>

        #include <ctime>

        using namespace std;

        int main(void)
        {

           int array[10];

           srand (time(0));

           for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++ )// inputting values into array[10]
           {
              array[i] = 1 + (rand()%100); //any random number between 1 - 100
           }
           cout <<" Before sorting " << " ---" <<endl;
           for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++ )// printing the values
           {
               cout  <<  array[i]<< endl;
           }


           int key ;
           int t;//for future purpose
           int compcount = 0;//to keep track of comparisons
           for (int j = 1; j<sizeof(array)/sizeof(array[0]);j++)
           {
               key = array[j];//assign to second element initially
               t = j-1;//assign to first element initially
               while (j > 0 && array[t]>key)
               {
                   array[t+1] = array[t];//moving array if bigger than next element
                   t = t-1;
                   compcount++;
               }
               array[t+1] = key;
           }
           cout << " After sorting" << " --- " <<endl;

           for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++ )
           {
               cout  <<  array[i]<< endl;
           }
           cout << "comparison count is " << compcount << endl;
           system ("pause");

           return 0;

        }

老实说,我有一个项目,它要求运行算法以获得最佳、最差和随机输入,并计算关键比较的次数(我认为在这段代码中是“compcount”)

现在随机输入对此有意义。当我用“一个已经排序的”数字数组(最好的情况)编写另一个代码时,键比较的次数为 0。 有人可以阐明最坏的情况是否正好相反?如果是这种情况,我尝试这样做,但我只得到了 32 次比较,而数组的大小为 32。

抱歉,问题太长了。 最坏情况输入应该有 (n^2-n)/2 比较次数对吗? 最好的情况应该是 n-1,因为只有第一个元素会遍历整个列表并确认它正在排序。我如何在代码中得到这个?

【问题讨论】:

  • 谁给了你这个代码?你测试过吗?排序部分肯定不正确。
  • 是的。我编写了这段代码并运行了它。它也工作得很好。
  • 不,不是。见here
  • 您应该考虑更好地命名变量。 j &gt; 0 在您的内部 while 循环中的值是多少?我没有看到它在你的 for 循环之外被弄乱了,这意味着它应该总是正确的。
  • 另外,您正在将比较作为 while 条件的一部分,这意味着您只计算成功比较,这就是为什么您的最佳案例结果是错误的。 compcount++ 也应该在 while 循环的正上方。

标签: c++ project insertion-sort


【解决方案1】:

你的程序有一个错误

           while (j > 0 && array[t]>key)

应该是

           while (t >= 0 && array[t]>key)

除此之外,它works for me 带有逆排序输入。这确实是最坏的情况,结果清楚地表明了这一点。

你得到了 n-1 的结果,但这是一个小问题。有关解决方案,请参阅@Mranz 的答案。

【讨论】:

  • 最坏情况的输入应该有 (n^2-n)/2 次比较吧?
  • @MahiVattekat:我们不希望t 使数组下溢。 j&gt;0 没有意义,因为 j 在循环期间没有改变。
  • 因为 t 实际上是递减的,因此您冒着将其递减到 0 以下的风险。
  • 是的,对不起,我刚刚意识到当我们确实在递减 t 时检查 j 是不必要的
  • 非常感谢!!!最坏情况输入确实是 (n^2-n)/2 次!!谢谢谢谢
【解决方案2】:

您将比较作为 while 条件的一部分进行,这意味着您只计算成功比较,这就是为什么您的最佳案例结果是错误的。 compcount++ 也应该在 while 循环的正上方。

编辑:

compCount++;
while (t >= 0 && array[t] > key)
{
    array[t+1] = array[t];//moving array if bigger than next element
    t = t-1;

    if (t >= 0) // This is a hack to ensure that 't >= 0' will pass
       compCount++; // and another comparison will happen
}

【讨论】:

  • 当我在 while 循环中使用 compcount++ 来应对最坏的情况时,它根据理论值实现。但是当我在 while 循环中为最佳情况执行 compcount ++ 时,它为 0。我是否应该在进行最佳情况输入时将 compcount++ 排除在 while 循环之外?
  • 这两个地方都需要。在 while 循环外捕获第一个比较,在 while 循环内捕获每个后续比较。这似乎是一个奇怪的流程,因为它确实如此,但它确实有效。
  • 如果是这种情况,最坏的情况是 527 次反对 496 .496 是正确的,当我为 32 输入 (n^2-n)/2 公式时。但理论上它不是 527。所以在最坏的情况下这不可能是真的
  • 哎呀,我是对的。它又是 527。所以它不能在两个地方
  • 你是对的。问题是您的比较是 while 循环的第二个条件。每次执行 full while 条件时,都需要增加 compCount。您可以将 compCount++ 包装在 while 循环中为:if (t &gt;= 0) { compCount++; }。这看起来很糟糕,您可能会考虑重新编写代码,但它会确保 compCount 只有在整个 while 条件被执行时才会增加。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 1970-01-01
  • 2015-08-22
  • 1970-01-01
  • 2013-02-21
  • 2021-07-28
相关资源
最近更新 更多