POV-Ray : Newsgroups : povray.off-topic : FizzBuzz : Re: Binary search Server Time
3 Sep 2024 23:24:59 EDT (-0400)
  Re: Binary search  
From: Orchid XP v8
Date: 4 Jul 2010 07:11:18
Message: <4c306c56$1@news.povray.org>
Orchid XP v8 wrote:

>   find :: (Ord y) => (x -> y) -> y -> IArray x -> (Int,Int) -> Maybe x
>   find field target array (base,size)
>     | size <  0 = error "invalid subrange size"
>     | size == 0 = Nothing
>     | size == 1 = if field (array ! base) == target then Just (array ! 
> base) else Nothing
>     | otherwise = find field target array (base, size `div` 2) `mappend` 
> find field target array (base + size `div` 2, size `div` 2)
> 
> Now I suppose I better go run that and see if it actually works.

Well, the final line is wrong for a start. (Due to rounding, if the 
array size isn't even, one element gets elided from the search.)

   | otherwise =
     let size2 = size `div` 2
     in  find field target array (base, size2) `mappend` find field 
target array (base + size2, size - size2)

That should work better. The division of a range into two subranges is 
still unequal, but at least all elements are included now...

-- 
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.