diff --git a/program/program/implement-selection-sort/implement_selection_sort.hs b/program/program/implement-selection-sort/implement_selection_sort.hs new file mode 100644 index 000000000..2bb75185d --- /dev/null +++ b/program/program/implement-selection-sort/implement_selection_sort.hs @@ -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