Question

In: Computer Science

How Many Comparisons would be made by binary when searching a list of one million elements...

How Many Comparisons would be made by binary when searching a list of one million elements in the best and worst case ?

Solutions

Expert Solution

Since only binary is mentioned in the question, I am assuming that question is asking about binary search. If there is any other interpretation, then please let me know in the comment section. I will update my answer accordingly.

The binary search algorithm works only on a sorted input list.
The algorithm compares the middle element of a list with the search key,
Case 1: If mid element == key, it returns mid
Case 2: If mid element < key, it discards left half list, and searches further only in the right half,
updates Low to mid+1
Case 3: If mid element > key, it discards right half list, and searches further only in the left half,
updates High to mid-1


For example: When searchin for key 13, in the list given in the figure, we compare 13 with the mid element 8,
Since mid(8) < 13, we further search only in the right half of the list. By updating Low to Mid + 1, Hence, the low will be at index 5, pointing to element 10.

Since, at each iteration we discard half of the list, In worst case we keep dividing the array until there is only one element left and then we stop. Worst case occurs when the search key is not found in the list.
Since we keep dividing input list of size N, into half until we have list with 1 element, the total number of comparisons in worst case = log2N+1, where N is the size of input list,

In best case, the binary search finds the element in one comparison,
If the mid element of the original list is the key to be searched, we can find it in just one comparison.

------------------------------------------------------------

Searching a list of one million elements:

N = 1000000

Best case = 1 comparison

Worst case = log21000000 + 1 =  21 comparisons

-------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a...
3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a binary string of 1000 zeros while trying to match the following patters: a.       1000 b.       0001 c.       00010
Given a list of 4096 sorted values, about how many comparisons can you expect to be...
Given a list of 4096 sorted values, about how many comparisons can you expect to be performed to look for a value that's not in the list using the bubble sort algorithm?
How many elements of order 2 are there in S5 and in S6? How many elements...
How many elements of order 2 are there in S5 and in S6? How many elements of order 2 are there in Sn? (abstract algebra)
Why would you select one paradigm over another? What are the benefits of each? When searching...
Why would you select one paradigm over another? What are the benefits of each? When searching for the research evidence – what elements of quality should be applied? Be sure to include references to support your statements.
There are many cryptocurrencies in the world today. If apple made a cryptocurrency, how would it...
There are many cryptocurrencies in the world today. If apple made a cryptocurrency, how would it compete to the rest? What marketing strategy could they use?
List the advice that Mark Kohn gives when searching for unreported income or hidden assets
List the advice that Mark Kohn gives when searching for unreported income or hidden assets
DISCRETE MATH If x has t elements and y has s elements, how many different one...
DISCRETE MATH If x has t elements and y has s elements, how many different one to one and onto functions are there, and what are they? Show your work
Transforming a List One typical kind of list processing is to transform the elements of a...
Transforming a List One typical kind of list processing is to transform the elements of a list, producing a copy of the list but with the elements transformed. Write a function definition of add_sizes using a for loop that takes a list of strings, say strings, and returns a list of pairs consisting of the elements of strings together with their lengths. def add_sizes(strings): """ Return the list of pairs consisting of the elements of strings together with their sizes....
QUESTION 3 The process of searching through many records in one or more databases looking for...
QUESTION 3 The process of searching through many records in one or more databases looking for patterns or relationships is called: A. credit reporting B. data mining C. information gathering D. microtargeting E. pattern matching
Suppose |A| = n. How many symmetric binary relations are there on A?
Suppose |A| = n. How many symmetric binary relations are there on A?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT