Consider a binary search algorithm as described in the modules. Assume that the array to be searched is pre-sorted, so that sort is not part of the search algorithm. Searching an array using a binary search (check all choices that apply):
A … is usually slower for each search than a simple linear search. |
B | … requires a pre-sorted array in order to work. |
C | … is usually faster for each search than a simple linear search. |
D | … requires more code and logic than a simple linear search. |
E | … does a sort as part of the search algorithm. |
Expert Answer
A is wrong binary search is faster than a simple linear search
B yes, it requires a pre-sorted array to work
C True , I have already said that it is faster, Binary search runs in at worst logarithmic time, making O(log n) comparisons, where n is the number of elements in the array, the O is Big O notation, and log is the logarithm.
in case of simple linear Linear search runs in at worst linear time and makes at most n comparisons, where n is the length of the list. If each element is equally likely to be searched, then linear search has an average case of n/2 comparisons, but the average case can be affected if the search probabilities for each element vary.
D yes , it requires more code and logic than a simple linear search
E No, it won’t sort the array while searching , and also it can only do in pre- sorted array