简体   繁体   English

最好的情况,最坏的情况,插入排序算法的随机数据输入?

[英]Best case , Worst case , Random data input for insertion sort algorithm?

This is the code I wrote for inputting random numbers initially and then sorting them with the insertion sort method. 这是我编写的用于初始输入随机数然后使用插入排序方法对其进行排序的代码。

        #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;

        }

I'll be honest , I have a project and it asks to run the algorithm for the best , worst and random input and calculate the number of key comparisons (Which I believe is "compcount" in this code) 老实说,我有一个项目,它要求运行算法以获得最佳,最差和随机输入,并计算关键比较的数量(我相信在此代码中是“compcount”)

Now random input would make sense for this. 现在随机输入对此有意义。 When I did another code with "an already sorted "array of numbers (the best case scenario) ,the number of key comparisons was 0. Can someone shed some light onto whether the worst case scenario is just the complete opposite of that? 当我使用“已经排序”的数字数组(最好的情况)进行另一个代码时,关键比较的数量为0.能否有人了解最坏情况是否与此完全相反? If that were the case I tried doing that but I only got 32 comparisons with the size of the array being 32. 如果是这种情况我尝试这样做,但我只有32个比较,数组的大小为32。

Sorry for the long question . 很抱歉这个问题很长。 Worst case input should have (n^2-n)/2 number of comparisons right? 最坏情况输入应该有(n ^ 2-n)/ 2个比较对吗? And the best case should have n-1 because only the first element would run through the entire list and confirm it being sorted.How do I get this in code? 最好的情况应该是n-1,因为只有第一个元素会遍历整个列表并确认它已经排序。我如何在代码中得到它?

You are making a comparison as part of the while conditional, which means you are only counting a successful comparison, which is why your best case result is wrong. 您正在进行比较作为while条件的一部分,这意味着您只计算成功的比较,这就是为什么您的最佳案例结果是错误的。 compcount++ should also be right above the while loop. compcount ++也应该在while循环之上。

Edit: 编辑:

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
}

There's one error in your program 你的程序中有一个错误

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

should be 应该

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

Other than that, it works for me with the inverse sorted input. 除此之外,它适用于反向排序输入。 It is indeed the worst case and the result clearly shows it. 这确实是最糟糕的情况,结果清楚地表明了这一点。

You've got the result off by n-1, but that's a minor problem. 你的结果是n-1,但这是一个小问题。 For a solution, see @Mranz's answer. 有关解决方案,请参阅@ Mranz的答案。

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

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