MCQ IN COMPUTER SCIENCE & ENGINEERING

COMPUTER SCIENCE AND ENGINEERING

ALGORITHMS

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
For a binary search algorithm to work, it is necessary that the array (list) must be
A
sorted
B
unsorted
C
in a heap
D
popped out of a stack
Explanation: 

Detailed explanation-1: -As binary search divides the list and selects a the sub-list to extend search based on comparison of values, it becomes necessary that the array (list) must be in sorted form.

Detailed explanation-2: -For this algorithm to work properly, the data collection should be in the sorted form. Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned.

Detailed explanation-3: -So, the answer is NO, it is not possible to use or implement Binary Search on unsorted arrays or lists, because, the repeated targeting of the mid element of one half depends on the sorted order of data structure.

Detailed explanation-4: -In order to perform Binary Search on ArrayList with Java Collections, we use the Collections. binarySearch() method. If key is not present, the it returns ((insertion point) + 1) *(-1).

Detailed explanation-5: -Answer. In Binary Search, the array is repeatedly divided into two halves and the element is searched in that half whose last element is greater than or equal to the element being searched. For this reason, Binary Search needs a sorted array to perform the search operation.

There is 1 question to complete.