Binary Search Algorithm

mbvissers.eth
Quick Programming
Published in
2 min readMar 26, 2021

No, you don’t need to know binary code for this.

Photo by Fabrice Villard on Unsplash

The binary search algorithm is a very popular and well-known algorithm for finding a specific value within a sorted array. It is also a popular algorithm to teach computer science studies and it isn’t too hard to grasp the basic principles of it.

The algorithm works on sorted arrays where it begins comparing the centermost value against the target value. If the target value is smaller than the centermost element the search continues in the lower half of the array where it will again compare the center value to the target.

This means that every time the target value isn't found, the size of the searchable array halves until two values are left.

By AlwaysAngry — Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=53687795

In the image, you can see that the value is found after four iterations. Each iteration halves the searchable range which gives this algorithm a worst-case performance of O(log N).

That is the binary search algorithm, thank you for reading and have a wonderful day.

--

--

mbvissers.eth
Quick Programming

I occasionally write about programming. Follow me on Twitter @0xmbvissers