Safe Haskell | None |
---|---|
Language | Haskell2010 |
Algorithms.BinarySearch
Synopsis
- binarySearch :: Integral a => (a -> Bool) -> a -> a -> a
- binarySearchUntil :: (Fractional r, Ord r) => r -> (r -> Bool) -> r -> r -> r
- binarySearchSeq :: (a -> Bool) -> Seq a -> Maybe Int
- binarySearchVec :: Vector v a => (a -> Bool) -> v a -> Maybe Int
Documentation
binarySearch :: Integral a => (a -> Bool) -> a -> a -> a Source #
Given a monotonic predicate p, a lower bound l, and an upper bound u, with: p l = False p u = True l < u.
Get the index h such that everything strictly smaller than h has: p i = False, and all i >= h, we have p h = True
running time: O(log(u−l))
binarySearchUntil :: (Fractional r, Ord r) => r -> (r -> Bool) -> r -> r -> r Source #
Given a value ε, a monotone predicate p, and two values l and u with:
- pl = False
- pu = True
- l<u
we find a value h such that:
- ph = True
- p(h−ε) = False
>>>
binarySearchUntil (0.1) (>= 0.5) 0 (1 :: Double)
0.5>>>
binarySearchUntil (0.1) (>= 0.51) 0 (1 :: Double)
0.5625>>>
binarySearchUntil (0.01) (>= 0.51) 0 (1 :: Double)
0.515625
binarySearchSeq :: (a -> Bool) -> Seq a -> Maybe Int Source #
Given a monotonic predicate, Get the index h such that everything strictly smaller than h has: p i = False, and all i >= h, we have p h = True
returns Nothing if no element satisfies p
running time: O(log2n+T∗logn), where T is the time to execute the predicate.
binarySearchVec :: Vector v a => (a -> Bool) -> v a -> Maybe Int Source #
Given a monotonic predicate, get the index h such that everything strictly smaller than h has: p i = False, and all i >= h, we have p h = True
returns Nothing if no element satisfies p
running time: O(T∗logn), where T is the time to execute the predicate.