473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Search for the nearest item by bsearch?

For example, I have a vector:
double vector[LENGTH]={1.11,2.38,4,53,17.14...,89.12,91.34}
And if the Key I want is 5.2, the nearest item will be 4,53.

I found that if the STEP of the vector is constant,
something like {1.1,1.2,1.3,1.4,...}
the compare function will be
int compare (const void * a, const void * b)
{

if (( *(double*)a - *(double*)b )>STEP/2)
return 1;
else if (( *(double*)a - *(double*)b )<-STEP/2)
return -1;
else
return 0;
}

But if the STEP of the vector isn't constant, how to get the compare
function?

Any suggestions will be appreciated!
Best regards,
Davy

Nov 15 '05 #1
4 3399
Davy wrote on 24/07/05 :
For example, I have a vector:
double vector[LENGTH]={1.11,2.38,4,53,17.14...,89.12,91.34}


I guess you wanted ... 2.38, 4.53, 17.14, ...

If not, the array is not sorted and bsearch() is not going to work.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #2
Davy wrote:
For example, I have a vector:
double vector[LENGTH]={1.11,2.38,4,53,17.14...,89.12,91.34}
And if the Key I want is 5.2, the nearest item will be 4,53.

I found that if the STEP of the vector is constant,
something like {1.1,1.2,1.3,1.4,...}
the compare function will be
int compare (const void * a, const void * b)
{

if (( *(double*)a - *(double*)b )>STEP/2)
return 1;
else if (( *(double*)a - *(double*)b )<-STEP/2)
return -1;
else
return 0;
}

But if the STEP of the vector isn't constant, how to get the compare
function?


The bsearch() function may not be the best tool for the
job. However, I think you can use it if you are willing to
pad your vector with two extra elements, one smaller than and
one larger than any possible key:

double vector[1+LENGTH+1] = {
-DBL_MAX, 1.11,2.38,...,91.34, DBL_MAX };

Since the comparison function's second argument points to one
of the elements of vector[] and since the key (by hypothesis)
is greater than vector[0] and less than vector[1+LENGTH], there
is always a neigboring vector[] element "in the right direction."
The comparison function can take advantage of this as follows:

int compare(const void *kp, const void *vp) {
double key = *(const double*)kp;
const double *vec = (const double*)vp;
if (key < vec[0]) {
if (key < (vec[-1] + vec[0]) / 2.0)
return -1;
}
else if (key > vec[0]) {
if (key > (vec[0] + vec[1]) / 2.0)
return +1;
}
return 0;
}

Note that this approach will find 1.11 as a "match" for
a key of minus ten million; bsearch() will find the closest
value in the array, but whether it is "close enough" is your
decision.

Personally, I think I'd dispense with bsearch() and just
write a suitable binary search in-line. It seems simpler and
more straightforward to write a search for "close" than to try
to fool a search that's looking for equality.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 15 '05 #3
Eric Sosman wrote:
Davy wrote:
But if the STEP of the vector isn't constant, how to get the compare
function?


The bsearch() function may not be the best tool for the job.


Indeed. I have found it useful to implement a bsearch variant
with a slightly-wider interface which enables it to report the
bounding items -- that is, in the case where the searched-for item
isn't found, to return pointers to the one or two surrounding items.

My implementation of this was sh-callable, not C-callable (see
http://www.eskimo.com/~scs/src/#bsearch, if you're curious), but
it would be useful to define and implement a pure C-callable variant.

Steve Summit
sc*@eskimo.com
Nov 15 '05 #4
Hi,

Thank you for your all help!
I have implement the method, and the search time accelerate 1000 times,
wow ;-)

All the best,
Davy

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gyromagnetic | last post by:
Hi, I have written a function that searches a text string for various words. The text is searched using a boolean 'and' or a boolean 'or' of the input list of search terms. Since I need to use...
1
by: Andy | last post by:
Say given the linearizer table and the key as tbl = {1, 10, 12, 25, 35, 47}; KEY = 1 Can Binary Search be used to find the enclosing segments? In this case KEY is enclosed by the segments 1 and...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
3
by: c_programmer | last post by:
I have a problem. There is a effective dated list FAMILY_ACCOUNTS stored in the memory. How to achieve the equivalent of the following SQL statement: select * from FAMILY_ACCOUNTS FA where...
1
by: maryjones11289 | last post by:
Hi All, I'm trying to write/find code that creates a Ternary Search Tree in Visual Basic (VB6 or .NET). Here's my situation: What I have is an array consisting of 60,000 string elements. All...
36
by: lovecreatesbeauty | last post by:
Any comments are welcome for following the two sequential search C functions of both integer and string versions. Though they are simple, please do not laugh at it :) Could you give your great...
3
by: Valvalis | last post by:
Hello, I am trying to set a class attribute of a text.item element to the value of its nearest ancestor. I want to do this in the case that the class of the text.item is currently a blank string....
4
by: Amandil | last post by:
Hi, all. I'd like to check whether a certain string (one that I got from a user, or read from a file) is contained in a table of strings. The format of the table is char *table = { "string1",...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.