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
  1. 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)

  2. 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

  3. 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

  4. Binary search algorithm can be applied to: a) Characters b) Integers c) Floats d) All of the above Answer: d) All of the above

  5. 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

  6. 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)

  7. 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

  8. What is the middle element in an array of size 10? a) 4 b) 5 c) 9 d) 10 Answer: b) 5

  9. 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

  10. 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
  1. 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.

  2. What is the time complexity of binary search algorithm? Answer: The time complexity of binary search algorithm is O(log n).

  3. Can binary search algorithm be applied to an unsorted array? Answer: No, binary search algorithm can only be applied to a sorted array.

  4. 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).

  5. 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.

  6. 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).

  7. What is the best-case time complexity of binary search algorithm? Answer: The best-case time complexity of binary search algorithm is O(1).

  8. 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.

  9. 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.

  10. 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.

Binary search is a search algorithm used to find the position of a target value in a sorted array. The algorithm works by repeatedly dividing the search interval in half until the target value is found or until the search interval becomes empty. The binary search algorithm has a time complexity of O(log n), which makes it very efficient for searching large arrays. To implement the binary search algorithm, the array must be sorted in ascending or descending order. The algorithm starts by comparing the target value with the middle element of the array. If the target value is equal to the middle element, then the search is complete. If the target value is greater than the middle element, then the search continues in the right half of the array. If the target value is less than the middle element, then the search continues in the left half of the array. Binary search can be implemented using iterative or recursive approaches. The iterative approach is more efficient than the recursive approach because it uses less memory and avoids the overhead of recursive function calls. To improve the performance of binary search, the array can be divided into blocks of fixed size. This technique is called block search or block binary search. In block search, the block containing the target value is located first, and then binary search is performed within that block. This technique reduces the number of comparisons required to find the target value, making the search faster. In conclusion, binary search is a very efficient algorithm for searching sorted arrays. It has a time complexity of O(log n), which makes it suitable for large arrays. The algorithm can be implemented using iterative or recursive approaches and can be optimized using block search.