Binary SearchBinary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted list of items. If the elements are not sorted already, we need to sort them first.
Input: arr[] = {10, 20, 30, 50, 60, 80, 110, 130, 140, 170}, x = 110
Output: 6
Element x is present at index 6
Below are the implementation of above algorithm Linear Search into programs
#include < stdio.h>
int binarySearch (int* A , int first , int last , int key )
{
int x, y, mid;
if (last < first)
return -1;
else
{
mid = (last + first) / 2;
if (A[mid] == key)
return mid;
x = binarySearch(A, first , mid - 1, key);
y = binarySearch(A, mid + 1, last, key);
if (x == -1 && y == -1)
return -1;
else if (x == -1 && y != -1)
return y;
else
return x;
}
}
void main ( )
{
int a[50] , index , size , key , i ;
clrscr( );
printf("Enter Array Size : " );
scanf("%d" , & size );
printf("Enter %d Elements \n " , size );
for ( i = 0; i < size ; i++)
scanf("%d" , &a[i]);
printf("Enter Element to be Searched : " );
scanf("%d" , &key );
index = binarySearch(a, 0, (size-1), key);
if(index==-1)
{
printf("Search unsuccessful\n");
printf("%d is not present in a given list" , key);
}
else
{
printf("Search successful\n");
printf("%d is present in a given list" , key);
}
getch( );
}
Code
// Java implementation of recursive Binary Search
class BinarySearch {
// Returns index of x if it is present in arr[l..
// r], else return -1
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the
// middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not present
// in array
return -1;
}
// Driver method to test above
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "
+ result);
}
}
Code
# Python3 Program for recursive binary search.
# Returns index of x in arr if present, else -1
def binarySearch(arr, l, r, x):
# Check base case
if r >= l:
mid = l + (r - l) // 2
# If element is present at the middle itself
if arr[mid] == x:
return mid
# If element is smaller than mid, then it
# can only be present in left subarray
elif arr[mid] > x:
return binarySearch(arr, l, mid-1, x)
# Else the element can only be present
# in right subarray
else:
return binarySearch(arr, mid + 1, r, x)
else:
# Element is not present in the array
return -1
# Driver Code
arr = [2, 3, 4, 10, 40]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index % d" % result)
else:
print("Element is not present in array")
Code