Binary Search Algorithm is a method for finding an element within a given data structure given that the list is sorted. It is more efficient than linear search because this algorithm always targets the mid element of the data structure and divide the search space into half untill the match is found.
Let's look at the working of this algorithm.
Do refer the code available the end of this section to understand the following theory.
PS: We give random inputs because the no. of input will generally be 100 or 1000 to find the time complexity and manually giving so many inputs is cumbersome work.
The basic operation in this algorithm is the comparision between each array element and the element to be searched(step 4.2 in the above working procedure). So increment the count variable above this if condition to get the count of no. of times the basic operation has been performed.
Time Complexity: O(logn)
Please open this in your pc or with a compatible app in your mobile.
C++ Implementation for BINARY SEARCHThat's it from this blog post. If you liked it then do share this blog with your friends or people who wanna get into programming world. Thank You!