47 lines
1.4 KiB
Forth
47 lines
1.4 KiB
Forth
namespace Sanchime.Algrithm.Sort
|
|
[<AutoOpen>]
|
|
module Merge =
|
|
|
|
let rec msort sorted = function
|
|
| [] -> []
|
|
| [x] -> [x]
|
|
| list ->
|
|
let evens, odds = divde list
|
|
in merge sorted (msort sorted evens, msort sorted odds)
|
|
|
|
and merge sorted = function
|
|
| xs, [] -> xs
|
|
| [], ys -> ys
|
|
| x::xs, y::ys ->
|
|
if sorted y x then y::(merge sorted (x::xs, ys))
|
|
else x::(merge sorted (xs, y::ys))
|
|
|
|
|
|
and divde = function
|
|
| [] -> [], []
|
|
| x::xs ->
|
|
let odds, evens = divde xs
|
|
in x::evens, odds
|
|
|
|
|
|
// let rec msort sorted = function
|
|
// | [] -> []
|
|
// | [x] -> [x]
|
|
// | xs ->
|
|
// let half = xs.Length / 2
|
|
// let left, right = List.splitAt half xs
|
|
// merge sorted (msort sorted left) (msort sorted right)
|
|
|
|
// and merge (sorted: 'a -> 'a -> bool) left right =
|
|
// let rec aux continuation left right =
|
|
// match left, right with
|
|
// | left, [] -> continuation left
|
|
// | [], right -> continuation right
|
|
// | x::xs, y::ys ->
|
|
// if sorted x y then
|
|
// aux (fun acc -> continuation (x::acc)) xs right
|
|
// else
|
|
// aux (fun acc -> continuation (y::acc)) left ys
|
|
// aux id left right
|
|
|
|
|