Skip to content

Instantly share code, notes, and snippets.

@msysyamamoto
Created October 22, 2012 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msysyamamoto/3935496 to your computer and use it in GitHub Desktop.
Save msysyamamoto/3935496 to your computer and use it in GitHub Desktop.
selection sort
import Data.List
import Test.QuickCheck
selectionSort :: Ord a => [a] -> [a]
selectionSort [] = []
selectionSort xs = v : (selectionSort (extract i xs))
where
(v, i) = findMin xs
findMin :: Ord a => [a] -> (a, Int)
findMin = minimumBy compare . numbering
numbering :: [a] -> [(a, Int)]
numbering xs = zip xs [0..]
extract :: Int -> [a] -> [a]
extract n xs = ys ++ stail zs
where
(ys, zs) = splitAt n xs
stail [] = []
stail (_:rs) = rs
prop_selectionSort :: [Int] -> Bool
prop_selectionSort xs = selectionSort xs == sort xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment