Data Structures and Algorithms : Binary Search

Ajay krishnan
3 min readApr 8, 2021

--

Binary search is an efficient algorithm for finding an item from a sorted list of items. It works by repeatedly dividing in half the portion of the list that could contain the item, until you’ve narrowed down the possible locations to just one.

Worst case complexity : O(log n)

Average case complexity : O(log n)

Best case complexity : O(1)

Space complexity : O(1)

Binary Search implementation in C programming language

The idea is given a sorted array, we look for the search element in the mid-location of the array to see if it is the one we are looking for.
If the element at the mid-location of the array is not the one we are looking for then compare the element in mid location with the search element.
If the search element is bigger than the mid-location element then continue search only in the array locations after the mid-location.
If the search element is smaller than the mid-location element then we continue our search only in locations before the mid-location

We can implement binary search in two way
1. Recursive implementation of binary search

C Program

https://github.com/Ajay-user/Data-Structures/blob/master/C/BinarySearch/binarySearch.c

C++

https://github.com/Ajay-user/Data-Structures/blob/master/CPP/BinarySearch/binarySearch.cpp

JAVA

https://github.com/Ajay-user/Data-Structures/blob/master/JAVA/BinarySearch/BinarySearch.java

JS

https://github.com/Ajay-user/Data-Structures/blob/master/JavaScript/BinarySearch/binarySearch.js

Python

https://github.com/Ajay-user/Data-Structures/blob/master/Python/BinarySearch/binarySearch.py

2. Iterative implementation of binary search

C Program

https://github.com/Ajay-user/Data-Structures/blob/master/C/BinarySearch/binarySearch.c

C++

https://github.com/Ajay-user/Data-Structures/blob/master/CPP/BinarySearch/binarySearch.cpp

JAVA

https://github.com/Ajay-user/Data-Structures/blob/master/JAVA/BinarySearch/BinarySearch.java

JS

https://github.com/Ajay-user/Data-Structures/blob/master/JavaScript/BinarySearch/binarySearch.js

Python

https://github.com/Ajay-user/Data-Structures/blob/master/Python/BinarySearch/binarySearch.py

Resources

Github link : https://github.com/Ajay-user/Data-Structures

Repls link : https://replit.com/@ajaykrishnan1

--

--

No responses yet