forked from hackprot1/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmerge-sort.hs
More file actions
27 lines (24 loc) · 1.03 KB
/
merge-sort.hs
File metadata and controls
27 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- # Binary Search in Haskell
-- This is an implementation of simple Binary Search in Haskell on any type that can be ordered.
-- Binary Search is an effective searching algorithm, that can be used to find an element in a sorted list in O(lg(n)) time.
-- Binary search with index carry
bsWithIndex :: (Ord a) => [a] -> a -> Int -> Maybe Int
bsWithIndex list n i
| n == head list = Just i
| len == 1 = Nothing -- only candidate in list is not the right elem
| n < head ys = bsWithIndex xs n i
| otherwise = bsWithIndex ys n (i + half)
where
len = length list
half = len `div` 2
(xs, ys) = splitAt half list
-- Binary search returning index, or -1 if element not in list
bs :: (Ord a) => [a] -> a -> Int
bs list n = case bsWithIndex list n 0 of
Just x -> x
Nothing -> -1
main :: IO ()
main = do
let intList = [1,4,7,10,25,30]
print $ bs intList 29 -- 29 -> -1 as not in list
print $ bs intList 7 -- 7 -> 2 as in list