Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Selection sort in Haskell (#5768)
Browse files Browse the repository at this point in the history
Co-authored-by: Riyazul555 <riyazulislam2003@gmail.com>
  • Loading branch information
Riyazul555 and MdRiyazulIslam authored Jul 2, 2024
1 parent da33008 commit 19733c4
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Selection sort in Haskell
selectionSort :: (Ord a) => [a] -> [a]
selectionSort [] = []
selectionSort xs = let minElem = minimum xs
rest = removeFirst minElem xs
in minElem : selectionSort rest

-- Function to remove the first occurrence of an element from a list
removeFirst :: (Eq a) => a -> [a] -> [a]
removeFirst _ [] = []
removeFirst y (x:xs)
| y == x = xs
| otherwise = x : removeFirst y xs

-- Example usage
main :: IO ()
main = do
let unsortedList = [64, 25, 12, 22, 11]
let sortedList = selectionSort unsortedList
putStrLn $ "Sorted list: " ++ show sortedList

0 comments on commit 19733c4

Please sign in to comment.