-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathSelectionSort.hs
More file actions
24 lines (19 loc) · 716 Bytes
/
SelectionSort.hs
File metadata and controls
24 lines (19 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module Sorts.SelectionSort where
listToSort :: [Int]
listToSort = [13, 2, 3, 14, 17, 4, 1, 5, 16, 12, 9, 10, 15, 8, 7, 11, 18, 19, 6, 20]
-- The selection sort function
selectionSort :: (Ord a) => [a] -> [a]
selectionSort [] = []
selectionSort (x:xs) =
let (y, ys) = leastUnsorted (x:xs)
in y : selectionSort ys
-- select least element from unsorted list, return it and the rest of unsorted list
leastUnsorted :: (Ord a) => [a] -> (a, [a])
leastUnsorted [x] = (x, [])
leastUnsorted (x:xs) =
let (y, ys) = leastUnsorted xs
in if x <= y then (x, xs) else (y, x:ys)
main :: IO ()
main = do
putStrLn $ "Unsorted: " ++ show listToSort
putStrLn $ "Sorted: " ++ show (selectionSort listToSort)