简体   繁体   English

比较中最差和最好的情况

[英]Worst and best case in making comparisons

Given two arrays of double values, sorted in ascending order, one with 100 elements, the other with 10 elements, how many comparisons will it take in an optimal algorithm to merge these arrays into one sorted array, in the best case and in the worst case? 给定两个按升序排列的double值数组,一个数组包含100个元素,另一个数组包含10个元素,采用最佳算法将这些数组合并成一个排序的数组(在最佳情况下和最坏情况下)需要进行多少次比较案件?

The answer was: 答案是:

Best Case: 10 Comparisons 最佳案例:10个比较

Worst Case: 109 Comparisons 最坏的情况:109比较

Before you tag me out as a homework question or a stupid question, hear me out. 在将我标记为家庭作业问题或愚蠢问题之前,请听我说完。 I'll explain my reasoning and I made the title as if anyone has similar questions regarding iterations/comparisons they can take a look at this. 我将解释自己的理由,并以标题为标题,就好像有人对迭代/比较有类似的问题,他们可以看看。

I understand the best case of 10 comparisons because all of the values in the shorter array have to be less than all the values of the larger array of 100 values. 我了解10个比较的最佳情况,因为较短数组中的所有值都必须小于100值较大数组中的所有值。 Then you'll only make one comparison and sort them. 然后,您将只进行一个比较并对其进行排序。

But for the worst condition I don't get 109 comparisons. 但对于最坏的情况,我无法进行109次比较。 I don't know whether the question is asking to compare it using merge sort where each single number from the short array would do around 7 comparisons (log2^100 = 6.64) and 7 times 10 is a 70 not 109. 我不知道问题是否要求使用合并排序进行比较,其中短数组中的每个数字都会进行7次比较(log2 ^ 100 = 6.64),而7乘以10则是70而不是109。

Am I doing something wrong? 难道我做错了什么? The answer to the question doesn't help either. 这个问题的答案也无济于事。 If you all can understand and explain that'd be appreciated. 如果大家都能理解和解释,将不胜感激。

Even with the answer I don't understand how the comparisons are made and what type of sorting is used, if one is used. 即使有了答案,我也不明白如何进行比较以及使用哪种类型的排序(如果使用了排序)。 Can someone look at it again? 有人可以再看一次吗?

For the worst case consider a scenario that 在最坏的情况下,请考虑以下情况:

Array-1 : 1,2,3,4,5,6,7,8,9,110 . Array-11,2,3,4,5,6,7,8,9,110
Array-2 : 10,11,12,13,14,15,.....,109 . Array-210,11,12,13,14,15,.....,109

Best approach to merge both of them is by comparing first elements of both of the array (if available) and then take the minimum one and proceed further in that array. 合并这两个数组的最佳方法是比较两个数组的第一个元素(如果有),然后取最小的一个并在该数组中继续进行。

So here if we count total comparisons, First 9 elements of Array-1 will be compared with first element of Array-2 because first element of Array-2 is larger than first 9 elements of Array-1 So Total Comparisons = 9 till now. 因此,这里,如果我们计数总的比较, First 9的元件Array-1将被与第一元件相比Array-2因为first元件Array-2大于first 9的元件Array-1所以Total Comparisons = 9到现在。

Now last element of array-1 is largest among all the elements so its obvious that It will come at last in merging Still we have all elements present in array-2 which will be compared with element of array-1 . 现在array-1最后一个元素在所有元素中最大,因此很明显它最终将在合并中出现array-2存在all元素,并将其与array-1元素进行比较。

So this would take total 100 comparisons. 因此,总共需要进行100比较。 Now Total Comparisons = 100 + 9 = 109 . 现在, Total Comparisons = 100 + 9 = 109 So in this case, worst case have at max 109 comparisons. 因此,在这种情况下,最坏情况下最多只能进行109比较。

EDIT: 编辑:

Code for Merging: 合并代码:

Merge(int array1[],int array2[])
{
    int array3[]=new int[array1.length+array2.length];

    int point1=0;  //indicating current element of array1
    int point2=0;  //indicating current element of array2
    int point3=0;  //indicating current element of resultant array3

    while(point1<array1.length && point2<array2.length)
    {
        if(array1[point1]>array2[point2]) //This is to be counted as a comparison
        {
            array3[point3]=array2[point2]; //Take element of array2
            point2++;   //move to the next element of array2
            point3++;   //move to the next element of array3
        }
        else
        {
            array3[point3]=array1[point1]; //Take element of array3
            point1++;  //move to the next element of array1
            point3++;  //move to the next element of array2
        }
    }

    while(point1<array1.length)
    {
        array3[point3]=array1[point1]; //Take element of array3
        point1++;  //move to the next element of array1
        point3++;  //move to the next element of array2
    }

    while(point2<array2.legnth) 
    {
        array3[point3]=array2[point2]; //Take element of array2
        point2++;   //move to the next element of array2
        point3++;   //move to the next element of array3    
    }
}

So here When we compare elements of both arrays, It is to be counted as a comparison. 所以在这里,当我们比较两个数组的元素时,将被视为比较。 For the best case, 最好的情况是

Array-1 = 1,2,3,4,5,6,7,8,9,10 . Array-1 = 1,2,3,4,5,6,7,8,9,10
Array-2 = 11,12,13,14,.....,110 . Array-2 = 11,12,13,14,.....,110

So here total comparison will be 10 and after 10 th comparison , Array-1 will become empty(All elements are taken). 因此,这里的总比较为10 ,经过10次比较, Array-1将为空(采用所有元素)。 So no further more comparison will be done. 因此,将不再进行更多比较。 Therefore total comparisons in best case is 10 . 因此,最佳情况下的总比较为10

If you want to print total comparison for any inputs, Make few changes in the algorithm. 如果要打印任何输入的总比较,请在算法中进行很少的更改。

Define one variable int total_comparisons=0; 定义一个变量int total_comparisons=0; and then everytime you do a comparison, Increment it. 然后每次进行比较时,都要增加它。 So make change in comparison condition: 因此,请更改比较条件:

if(total_comparisons++ && array1[point1]>array2[point2]) //This is to be counted as a comparison

Then print total_comparisons at last. 然后最后打印total_comparisons This is better way for you to understand the logic. 这是您了解逻辑的更好方法。

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

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