## Binary search algorithm – C program

Binary search algorithm is generally done on ascending sorted array of items, for example Phone Diary or Dictionary. We will also look at C program for binary search. If we want to find H in dictionary then we will go middle of dictionary, if index is M or N then we know right side of dictionary is worthless to search hence problem is reduced to half. Then again we search at middle of left side of dictionary i.e G now we know that its at right side of G hence again problem is reduced by half.

This way even if problem size rises by double our work only increase by one more time. As binary search algorithm always reduces problem set in halve it takes in best case time of O(1) while in worst case time of O(log(n)) .The program logic for the same would e like this

while( first <= last ) { if ( array[middle] < search ) first = middle + 1; else if ( array[middle] == search ) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; }

Good write-up, I am normal visitor of one

Wow! Thank you! I constantly needed to write on my site something like that. Can I implement a part of your post to my website?

That is the proper weblog for anyone who needs to seek out out about this topic. You understand so much its almost exhausting to argue with you (not that I actually would want). You undoubtedly put a brand new spin on a topic thats been written about for years. Nice stuff, just nice!