-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathPancake_Sort.fs
More file actions
29 lines (21 loc) · 797 Bytes
/
Pancake_Sort.fs
File metadata and controls
29 lines (21 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace Algorithms.Sort
module PancakeSort =
let show data =
data |> Array.iter (printf "%d ")
printfn ""
let split (data: int []) pos = data.[0..pos], data.[(pos + 1)..]
let flip items pos =
let lower, upper = split items pos
Array.append (Array.rev lower) upper
let sort items =
let rec loop data limit =
if limit <= 0 then
data
else
let lower, upper = split data limit
let indexOfMax =
lower |> Array.findIndex ((=) (Array.max lower))
let partialSort =
Array.append (flip lower indexOfMax |> Array.rev) upper
loop partialSort (limit - 1)
loop items ((Array.length items) - 1)