Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Gaming > Development Programming Algorithms > Binary search w...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 665 of 670
Post > Topic >>

Binary search with timestamps

by Makhno <root@[EMAIL PROTECTED] > Feb 12, 2008 at 09:27 PM

Hello!
I've been using a binary-search like algorithm for an ordered array of 
time-stamps for ages. It works a bit like this (some code removed for 
clarity):


array - array to be searched, where time member is ordered
count - size of array
t - time
i - desired index where (array[i].time<t<array[i+1].time)


int i=count/2,n=count;
for(;;)
{
   n=n/2;
   if (t>array[i].time)
     i+=n;
   else
     i-=n;
   if (n<=1)
     return i;
}

It 'usually' works, but I can always find conditions where it doesn't 
and I'm not sure why. It works fine for sorted integers.

Noting the wikipedia page on binary search, it seems most programmers 
mis-program a binary so I've probably made the same mistake!

A linear search like this always gives me the right answer, but takes 
longer:


for(int i=0;i<count;++i)
{
   if (t>array[i].time && t<=array[i].time)
     return i;
}

I need speed though!

Thanks.




 11 Posts in Topic:
Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-12 21:27:40 
Re: Binary search with timestamps
Luc The Perverse <atay  2008-02-12 18:21:45 
Re: Binary search with timestamps
Miss Elaine Eos <Misc@  2008-02-13 06:15:07 
Re: Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-13 20:34:36 
Re: Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-13 21:07:58 
Re: Binary search with timestamps
Miss Elaine Eos <Misc@  2008-02-14 05:05:19 
Re: Binary search with timestamps
Luc The Perverse <atay  2008-02-15 08:35:05 
Re: Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-18 22:24:10 
Re: Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-13 20:42:16 
Re: Binary search with timestamps
Luc The Perverse <atay  2008-02-12 18:24:50 
Re: Binary search with timestamps
Makhno <root@[EMAIL PR  2008-02-13 20:51:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 7:19:00 CDT 2008.