简体   繁体   中英

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?

The answer was:

Best Case: 10 Comparisons

Worst Case: 109 Comparisons

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. Then you'll only make one comparison and sort them.

But for the worst condition I don't get 109 comparisons. 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.

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-2 : 10,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.

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 .

So this would take total 100 comparisons. Now Total Comparisons = 100 + 9 = 109 . So in this case, worst case have at max 109 comparisons.

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-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). So no further more comparison will be done. Therefore total comparisons in best case is 10 .

If you want to print total comparison for any inputs, Make few changes in the algorithm.

Define one variable 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. This is better way for you to understand the logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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