Here is a C function
int binarySearch(int arr[],int size, int item)
{
int left, right, middle;
left = 0;
right = size-1;
while(left <= right)
{
middle = ((left + right)/2);
if(item == arr[middle])
{
return(middle);
}
if(item > arr[middle])
{
left = middle+1;
}
else
{
right = middle-1;
}
}
return(-1);
}
Note that the Binary Search algorithm has a prerequisite that the array passed to it must be already sorted in ascending order. This will not work on an unsorted array. The complexity of this algorithm is O(log(n)).
Monday, July 9, 2007
Subscribe to:
Post Comments (Atom)
17 comments:
This code suffers from the usual problems of seemingly straightforward implementations of binary search: two instead of one comparisons inside the loop and problems with integer overflows for large arrays.
The following code I believe to be more robust and efficient (if you don't have C++/templates, just replace keyType with whatever is needed):
template<typename keyType> size_t binarySearch (keyType* s, size_t h, keyType n) {
if (h) {
size_t l=0;
while (l < h) {
size_t m = l + (h-l) / 2;
if (s [m] < n) {
l = m + 1;
} else {
h = m;
}
}
if (s [l] == n) {
return l;
}
}
return ~0;
}
I much prefer the tail recursion implementation, as its simpler to understand, and the compiler will transform it to an iterative version for performance.
bool findNumber(int num, int a[], int start, int end)
{
if (start == end) return a[start] == num;
int mid = ((end - start) / 2) + start;
return findNumber(num, a, start, mid) | findNumber(num, a, mid+1, end);
}
bool findNumber(int num, int a[], int len)
{
return findNumber(num, a, 0, len-1);
}
Thanks for the info. It is very helpful.
RegardsEducational site
Get jobs info at Educational site
I am not sure where you're getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for magnificent info I was looking for this info for my mission.
My homepage; diets that work fast
This paragraph provides clear idea for the new viewers of blogging, that truly how to do blogging and site-building.
Feel free to surf to my homepage :: 2013 at 12:01 AM
Wow that was odd. I just wrote an really long comment but
after I clicked submit my comment didn't appear. Grrrr... well I'm not
writing all that over again. Regardless, just wanted to say fantastic blog!
Feel free to surf to my web site :: graduate certificate online
Gгeat article, exactly ωhat I was looκing for.
Here is my web-ѕite: raspberry ketone uk
I гead this artіclе fullу concerning the resemblancе οf moѕt up-to-date and eaгlieг tеchnоlogіes, it's awesome article.
Also visit my site; vitamin shoppe multivitamins one daily
Hello to all, the contents existing at this web page are in fact remarkable for people experience,
well, keep up the nice work fellows.
Also visit my web page - healthy waist to height ratio
I am regular visitor, how are you everybody? This paragraph
posted at this website is genuinely nice.
My site Buy Raspberry ketone Plus
I usually do not drop a leave a response, however I read a great deal of responses
on this page "Write C code to implement the Binary Search algorithm.".
I do have 2 questions for you if it's allright. Could it be only me or does it look like like a few of these remarks appear as if they are written by brain dead folks? :-P And, if you are writing at additional places, I would like to keep up with you. Could you make a list of the complete urls of your social networking pages like your linkedin profile, Facebook page or twitter feed?
My weblog; Nuvo Cleanse
It's impressive that you are getting ideas from this paragraph as well as from our dialogue made at this place.
Feel free to surf to my homepage - Beyond Raspberry Ketone Reviews
Grеetingѕ! I κnoω thіs is kinda off topіc hoωеveг Ι'd figured I'd ask.
Would you be inteгested in tradіng links or maуbe guest authοгing a blog pοst οr νice-verѕa?
Mу sitе gоes ονer a lot of the same tοpics
as youгs anԁ Ι feеl we cоulԁ greаtly benеfit from each οther.
If уou might be іntereѕtеd feel free tο
send mе an e-maіl. I look foгward
tο hearіng fгοm you!
Tеrгific blog bу the way!
Αlsο viѕit my ωebpagе
- raspberry ketone
Hello my family member! I wish to say that this post
is amazing, nice written and include approximately all important infos.
I'd like to see extra posts like this .
My website :: Rvtl Anti Aging Solutions
Hello to every single one, it's truly a pleasant for me to pay a visit this web site, it contains important Information.
Also visit my weblog ... Skin care
Good day! Would you mind if I share your blog with my facebook
group? There's a lot of people that I think would really appreciate your content. Please let me know. Thanks
Feel free to visit my web page ... Testforce Xtreme
Way cool! Some extremely valid points! I appreciate you writing this article
and also the rest of the site is really good.
my site; Buy zinncollection
Post a Comment