POV-Ray : Newsgroups : povray.off-topic : FizzBuzz : Re: Binary search Server Time
4 Sep 2024 05:17:37 EDT (-0400)
  Re: Binary search  
From: Slime
Date: 4 Jul 2010 17:48:53
Message: <4c3101c5$1@news.povray.org>
> 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 =
 > let size2 = size `div` 2
 > in find field target array (base, size2) `mappend` find field target
 > array (base + size2, size - size2)

I don't know Haskell, but my best guess at understanding this code looks 
like you're always recursing into both sides, without actually doing the 
comparison of the center element to the target element. So you're not 
saving any time, and you would get faster results with a linear search. 
But maybe I just don't understand the language well enough to see the 
comparison.

  - Slime


Post a reply to this message

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