輸入:未排序列表/輸出:排序列表Ocaml插入排序
我的基本想法是在排序列表中插入一個整數。
(I可以對列表進行排序,如果我能插入所述第一元件到排序尾巴。)
我使用的「插入」,這是thehelper功能。
但是,它會溢出。有人告訴我問題是什麼?
let rec sort (l: int list) : int list =
match l with
[]->[]
| x::[]->[x]
| x1::x2::xs->let rec insert (n,dest) =
match dest with
[]->[n]
| y::[]-> if n<y then [n;y] else [y;n]
| y1::y2::ys-> if y1<y2 then n::dest else y2::insert(y1,xs)
in insert(x1,sort(x2::xs)) ;;
你的建議是完美的。非常感謝傑弗裏。 – 2013-04-10 02:14:14