binary search
- From: "Roman Mashak" <mrv@xxxxxxxx>
- Date: Thu, 29 Nov 2007 13:12:52 -0800
Hello,
I'm trying to do simple implementation of 'binary search' algorithm. After
having read http://en.wikipedia.org/wiki/Binary_search I wrote this and got
stuck:
#include <stdio.h>
#include <stdlib.h>
static const double power[MAX] = {
-85.0, -86.0, -87.0, -88.0, -89.0, -90.0, -91.0, -92.0,
-93.0, -94.0, -95.0, -96.0, -97.0, -98.0, -99.0, -100.0
};
static int b_search(const double data[], double value, double low, double
high)
{
int mean;
if (high < low)
return -1;
mean = (low + high) / 2;
if (data[mean] > value)
return b_search(data, value, low, mean - 1);
else if (data[mean] < value)
return b_search(data, value, mean + 1, high);
else
return mean;
}
int main(void)
{
b_search(power, -97.0, -100.0, -85.0);
return 0;
}
It's compiled correctly, but I doubt I'm passing array as parameter in a
right way (what is more correct?). And second, considering that values in
array are negative, 'mean' is calculated wrong ((-100 + (-85)) / 2 = -92.5),
or I'm not understanding the algorithm?
Please clarify these points. Thanks.
With best regards, Roman Mashak. E-mail: mrv@xxxxxxxx
.
- Follow-Ups:
- Re: binary search
- From: CBFalconer
- Re: binary search
- From: Travis Vitek
- Re: binary search
- Prev by Date: Re: binary search
- Next by Date: Re: Multi precision floating point
- Previous by thread: malloc realloc and pointers
- Next by thread: Re: binary search
- Index(es):
Relevant Pages
|