Update selection sort

This commit is contained in:
seanyu4296
2020-08-05 12:58:43 +00:00
parent 23f31b4f3f
commit 2f166a0ee1

View File

@@ -1,39 +1,17 @@
module GrokkingAlgos.SelectionSort where
import Prelude
import Data.Foldable (minimum)
import Data.List (List(..), delete, (:))
import Data.Maybe (Maybe(..), fromJust)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Class.Console (logShow)
import Partial.Unsafe (unsafePartial)
-- PARTIAL
selectionsort :: List Int -> List Int
selectionsort Nil = Nil
selectionsort l = case minimum l of
Nothing -> Nil
Just min -> min : selectionsort (delete min l)
selectionsort l = minVal : (selectionsort rest)
where
minVal :: Int
minVal = unsafePartial $ fromJust $ minimum l
rest :: List Int
rest = delete minVal l
-- TOTAL
selectionsort' :: List Int -> Maybe (List Int)
selectionsort' Nil = Just $ Nil
selectionsort' l = do
mval <- minimum l
let
rest = delete mval l
restSort <- selectionsort' rest
pure $ mval : restSort
--
main :: Effect Unit
main = do
logShow $ selectionsort $ 1 : 2 : 3 : 5 : 4 : Nil
logShow $ selectionsort' $ 1 : 2 : 3 : 5 : 4 : Nil