Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Search/Haskell/binarySearch.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
binarySearch :: [Int] -> Int -> Int
binarySearch list element =
binarySearch' list 0 (size - 1) element
where size = length list

binarySearch' :: [Int] -> Int -> Int -> Int -> Int
binarySearch' list left right element
| right < left = -1
| (list !! middle) == element = middle
| (list !! middle) < element = binarySearch' list (middle + 1) right element
| otherwise = binarySearch' list left (middle - 1) element
where middle = (left + right) `div` 2
5 changes: 5 additions & 0 deletions Search/Haskell/linearSearch.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linearSearch :: [Int] -> Int -> Int
linearSearch [] _ = -1
linearSearch (head: tail) element
| head == element = element
| otherwise = linearSearch tail element