RSS Feed
online prescription solutions
online discount medstore
pills online
buy lorazepam without prescription
xanax for sale
buy xanax without prescription
buy ambien without prescription
ambien for sale
buy modafinil without prescription
buy phentermine without prescription
modafinil for sale
phentermine for sale
lorazepam for sale
buy lexotan without prescription
bromazepam for sale
xenical for sale
buy stilnox without prescription
valium for sale
buy prosom without prescription
buy mefenorex without prescription
buy sildenafil citrate without prescription
buy adipex-p without prescription
librium for sale
buy restoril without prescription
buy halazepam without prescription
cephalexin for sale
buy zoloft without prescription
buy renova without prescription
renova for sale
terbinafine for sale
dalmane for sale
buy lormetazepam without prescription
nobrium for sale
buy klonopin without prescription
priligy dapoxetine for sale
buy prednisone without prescription
buy aleram without prescription
buy flomax without prescription
imovane for sale
adipex-p for sale
buy niravam without prescription
seroquel for sale
carisoprodol for sale
buy deltasone without prescription
buy diazepam without prescription
zopiclone for sale
buy imitrex without prescription
testosterone anadoil for sale
buy provigil without prescription
sonata for sale
nimetazepam for sale
buy temazepam without prescription
buy xenical without prescription
buy famvir without prescription
buy seroquel without prescription
rivotril for sale
acyclovir for sale
loprazolam for sale
buy nimetazepam without prescription
buy prozac without prescription
mogadon for sale
viagra for sale
buy valium without prescription
lamisil for sale
camazepam for sale
zithromax for sale
buy clobazam without prescription
buy diflucan without prescription
modalert for sale
diflucan for sale
buy alertec without prescription
buy zyban without prescription
buy serax without prescription
buy medazepam without prescription
buy imovane without prescription
mefenorex for sale
lormetazepam for sale
prednisone for sale
ativan for sale
buy alprazolam without prescription
buy camazepam without prescription
buy nobrium without prescription
mazindol for sale
buy mazindol without prescription
buy mogadon without prescription
buy terbinafine without prescription
diazepam for sale
buy topamax without prescription
cialis for sale
buy tafil-xanor without prescription
buy librium without prescription
buy zithromax without prescription
retin-a for sale
buy lunesta without prescription
serax for sale
restoril for sale
stilnox for sale
lamotrigine for sale

Binary Search y’all.

Posted on Sunday, April 25, 2010 in General

I’ll keep this short. I saw the first part of Kellan’s post today and decided to try my hand at it. I haven’t looked at his solution yet. Presented here is my attempt at a binary search in Ruby. I made two – one recursive for fun. I haven’t done any testing on this yet, so it might be broken. If it is… well being in with 90% of everyone else isn’t so bad is it?… is it? :)

One thing I was unclear on (going of the definition from the original post) was if the return should be the position in the array, a boolean or the searched-for element… I went with the last.

(if the embed doesn’t work, here they are on gist: http://gist.github.com/378840)

UPDATE: I tested the code out after posting it here (and have included my test suite in the embeded gist). I found a stupid typo bug in the non-recursive version:

- r = mid[mid] > needle ? 0...mid : mid+1..-1
+ r = valid_range[mid] > needle ? 0...mid : mid+1..-1

I’m going to try to think of a few more test cases before I call the matter closed. Kind of disappointed I made such a silly mistake :/

UPDATE 2: I added a pretty big test suite that someone wrote for this exercise. After fixing my very silly flub (in the first UPDATE), all the tests pass:

4 tests, 8206 assertions, 0 failures, 0 errors

I don’t know if I can give myself FULL credit, but the recursive one was right at first submission, so let’s say 95% ;)

UPDATE 3: GAHH! After benchmarking, I realized that this code isn’t O Log(N)… lame lame lame. I am not in the 10% at all ;). I’m pretty sure that Array#slice (which haystack[x...y] is a shortcut for) is O(N).
I’ve updated my gist with a better implementation (written after I looked at Kellan’s and others) along with benchmarks showing it’s O Log(N) (or thereabouts).

Bring on the comments

  1. Phil Taylor says:

    Nice algorithm. Thanks for the implementation!

Leave a Reply