Created
October 22, 2012 23:30
-
-
Save msysyamamoto/3935496 to your computer and use it in GitHub Desktop.
selection sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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