Update selection sort
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user