Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F# implementation. #64

Merged
merged 1 commit into from Aug 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions f#/StalinSort.fs
@@ -0,0 +1,31 @@
open System

module Seq =
let stalinSort s =
// `acc` must be non-empty
let rec impl acc rest =
if (Seq.isEmpty rest) then acc
elif (Seq.last acc) <= (Seq.head rest) then impl (Seq.append acc (Seq.singleton (Seq.head rest))) (Seq.tail rest)
else impl acc (Seq.tail rest)

if (Seq.isEmpty s) then s
else impl (Seq.singleton (Seq.head s)) (Seq.tail s)


[<EntryPoint>]
let main argv =
let test s =
printfn "before:"
s |> Seq.iter (printf "%d ")
printfn ""

printfn "after:"
(Seq.stalinSort s) |> Seq.iter (printf "%d ")
printfn ""

test [1; 2; 4; 3; 8; 0; 9; 5; 7]
printfn ""

test [1; 2; 4; 4; 3]

0