Skip to content

TheQueryCrew/P1-BinarySearch

Repository files navigation

Binary Search

Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].

A simple approach is to do a linear search. The time complexity of the Linear Search algorithm is O(n). Another approach to perform the same task is using Binary Search.

Binary Search:

Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array.
If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half.
Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Input:

arr[] = { 2, 3, 4, 10, 40 }
ValueToFind = 10

Output:

Index = 3

Time Complexity:

O(log n)

Releases

No releases published

Packages

No packages published

Contributors 10