39 Lecture
CS301
Midterm & Final Term Short Notes
Searching an Array: Binary Search
Binary search is a popular algorithm used for searching a sorted array of elements. It works by repeatedly dividing the array in half until the target element is found or determined to be absent. This search algorithm is highly efficient, as it
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the time complexity of binary search algorithm? a) O(1) b) O(n) c) O(log n) d) O(n^2) Answer: c) O(log n)
In which type of array is binary search the most efficient? a) Sorted array b) Unsorted array c) Randomly sorted array d) None of the above Answer: a) Sorted array
Binary search algorithm can be used for: a) Array b) Linked list c) Both A and B d) None of the above Answer: a) Array
Binary search algorithm can be applied to: a) Characters b) Integers c) Floats d) All of the above Answer: d) All of the above
Which of the following is not a step in binary search algorithm? a) Check if the middle element is equal to the target element b) If the target element is greater than the middle element, search the left half of the array c) If the target element is less than the middle element, search the right half of the array d) Return the index of the target element Answer: d) Return the index of the target element
What is the worst-case time complexity of binary search algorithm? a) O(1) b) O(n) c) O(log n) d) O(n^2) Answer: c) O(log n)
Which of the following is not a requirement for binary search algorithm to work? a) The array must be sorted b) The array must be in ascending order c) The array must be in descending order d) The array must be homogeneous Answer: c) The array must be in descending order
What is the middle element in an array of size 10? a) 4 b) 5 c) 9 d) 10 Answer: b) 5
How many elements are left in the array after the first iteration of binary search on an array of size 16? a) 8 b) 4 c) 2 d) 1 Answer: a) 8
What is the index of the target element in the array [1, 3, 5, 7, 9] when using binary search to find 7? a) 2 b) 3 c) 4 d) 5 Answer: b) 3
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
Explain the binary search algorithm. Answer: Binary search is a search algorithm that finds the position of a target value within a sorted array. It starts by comparing the target value with the middle element of the array. If they match, the search is successful. Otherwise, if the target value is less than the middle element, it searches the left half of the array. If the target value is greater than the middle element, it searches the right half of the array. This process repeats until the target value is found or until the subarray is empty.
What is the time complexity of binary search algorithm? Answer: The time complexity of binary search algorithm is O(log n).
Can binary search algorithm be applied to an unsorted array? Answer: No, binary search algorithm can only be applied to a sorted array.
What is the difference between linear search and binary search? Answer: Linear search is a search algorithm that checks each element of an array until it finds the target value, while binary search is a search algorithm that cuts the array in half at each step until it finds the target value. Linear search has a time complexity of O(n), while binary search has a time complexity of O(log n).
How does binary search algorithm work on a linked list? Answer: Binary search algorithm cannot be applied directly to a linked list, as it requires random access to elements. However, if the linked list is sorted and converted into an array, binary search can be applied.
What is the worst-case time complexity of binary search algorithm? Answer: The worst-case time complexity of binary search algorithm is O(log n).
What is the best-case time complexity of binary search algorithm? Answer: The best-case time complexity of binary search algorithm is O(1).
Can binary search algorithm be used to find the second occurrence of a target value in an array? Answer: Yes, binary search algorithm can be modified to find the second occurrence of a target value in an array.
What happens if the target value is not found in the array during binary search? Answer: If the target value is not found in the array during binary search, the algorithm returns -1 or some other signal to indicate that the target value is not present in the array.
What is the importance of a sorted array in binary search algorithm? Answer: Binary search algorithm requires a sorted array because it relies on the property that the middle element of a sorted array divides the array into two halves. If the array is not sorted, this property does not hold, and binary search cannot be applied.