Binary Search using Unsorted Array

# Our Objective

To implement a binary search algorithm in Python

# The Theory

Binary search is a searching algorithm that works by repeatedly dividing the search interval in half. It requires a sorted array to work on. It begins by comparing the middle element of the array with the target element. If the middle element is equal to the target element, the search is successful and the algorithm returns the index of the middle element. If the target element is greater than the middle element, the search continues on the right half of the array. Otherwise, the search continues on the left half of the array.

The function 'search' in the code takes in four arguments: the array to be searched, the starting index of the search interval, the ending index of the search interval, and the target element.

The function first checks if the starting index is equal to the ending index. If it is, it checks if the element at that index is equal to the target element. If it is, it returns 'true'. Otherwise, it returns 'false'.

If the starting index is not equal to the ending index, the function calculates the middle index of the search interval using integer division (//) and calls itself recursively on the left half and right half of the search interval. If the target element is found in either the left or right half of the search interval, the function returns 'true'. Otherwise, it returns 'false'.

The given code then creates a list 'a' of integers and calls the 'search' function with the arguments a, 0, len(a-1), and 231. This will search for the element 231 in the list 'a'. The return value of the function is then printed. If the element is found in the list, the function will return 'true', otherwise it will return 'false'.

# Learning Outcomes

• Understanding of binary search algorithm: The code demonstrates the implementation of a binary search algorithm, which is an efficient algorithm for searching a sorted array.
• Recursion: The implementation of binary search in the code uses a recursive approach, which involves a function calling itself repeatedly until a base condition is met. This helps in understanding the concept of recursion and its use in programming.
• Divide and conquer strategy: Binary search algorithm uses a divide-and-conquer strategy to search for an element in a sorted array, by dividing the array into smaller halves and eliminating one half of the search space at each iteration. This helps in understanding the concept of divide and conquer and its application in programming.
• Python programming language: The code is implemented in Python, which is a widely used programming language known for its simplicity and readability. By studying the code, one can learn about the syntax and structure of Python programming language.
• Problem solving: The implementation of binary search algorithm requires problem-solving skills, as it involves understanding the problem, designing an algorithm, and implementing it in code. This helps in developing problem-solving skills that can be applied to other programming problems as well.