Binary Search Sorted Array

# Our Objective

To implement a program to search an element in a list using binary search technique.

# The Theory

This program implements the binary search algorithm to find the index of a given element 'x' in a sorted array 'arr'.

The binary search algorithm works by repeatedly dividing the search interval in half until the target element is found or the search interval is empty. At each step, the algorithm compares the middle element of the current search interval with the target element 'x'. If they match, the algorithm returns the index of the middle element. If the middle element is greater than the target element, the search interval is divided into the lower half, and the search continues in that half. If the middle element is smaller than the target element, the search interval is divided into the upper half, and the search continues in that half. The process repeats until the target element is found or the search interval is empty.

In this implementation, the binary_search() function takes the input array 'arr', the indices of the lower and upper bounds of the search interval, and the target element 'x'. The function first checks if the search interval is not empty, i.e., if the 'high' index is greater than or equal to the 'low' index. If the search interval is not empty, the function calculates the middle index of the search interval using integer division. If the middle element is equal to the target element 'x', the function returns the index of the middle element. If the middle element is greater than the target element 'x', the function calls itself recursively with the lower half of the search interval, i.e., the 'low' index and 'mid-1'. If the middle element is smaller than the target element 'x', the function calls itself recursively with the upper half of the search interval, i.e., the 'mid+1' and 'high' index. The recursion continues until the target element is found, or the search interval is empty, in which case the function returns -1.

Finally, the program calls the binary_search() function with the input array 'arr', the indices of the lower and upper bounds of the search interval, and the target element 'x'. If the binary_search() function returns an index other than -1, the program prints the message "Element is present at index" followed by the index of the target element in the input array. Otherwise, the program prints the message "Element is not present in array".

# Learning Outcomes

By learning the Divide and Conquer algorithm to find the maximum value of an array, you can achieve several learning outcomes, including:

• Understanding of the binary search algorithm: The program implements the binary search algorithm, which is an efficient way of searching for an element in a sorted array. Studying this program will help the learner understand the basic workings of the algorithm, its time complexity, and how it compares to other search algorithms.
• Familiarity with recursion: The binary_search() function in the program uses recursion to implement the binary search algorithm. By studying this program, the learner will gain a better understanding of how recursion works and how it can be used to solve problems.
• Ability to implement binary search algorithm in code: The program provides a practical example of how to implement the binary search algorithm in code using recursion. The learner will be able to apply this knowledge to implement the algorithm in their own programs and solve search problems efficiently.
• Understanding of the importance of sorting in search algorithms: The program assumes that the input array is sorted. Studying this program will help the learner understand the importance of sorting in search algorithms and how it affects the time complexity of the algorithm.
• Debugging skills: The program provides an opportunity for the learner to practice their debugging skills. By studying the code and identifying any errors or bugs, the learner will develop their ability to read and understand code and troubleshoot issues in their own programs.