Sanchime.Learn/Algrithms/Sorts/InsertSort.fs

14 lines
400 B
Forth
Raw Permalink Normal View History

2022-07-05 20:26:22 +08:00
namespace Sanchime.Algrithm.Sort
[<AutoOpen>]
module Insert =
let rec isort sorted = function
| [] -> []
| [x] -> [x]
| x::xs -> xs |> isort sorted |> insert sorted x
// 将一个元素插入至列表开头
and insert sorted value = function
| [] -> [value]
| x::xs -> if sorted value x then value::x::xs else x :: insert sorted value xs