Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F#排序问题_F#_Functional Programming - Fatal编程技术网

F#排序问题

F#排序问题,f#,functional-programming,F#,Functional Programming,这个代码有什么问题?为什么它不会排序 let rec sort = function | [] -> [] | [x] -> [x] | x1::x2::xs -> if x1 <= x2 then x1 :: sort (x2::xs) else x2 :: sort (x1::xs) let rec sort=函数 | [] -> [] |[x]->

这个代码有什么问题?为什么它不会排序

let rec sort = function
  | []         -> []
  | [x]        -> [x]
  | x1::x2::xs -> if x1 <= x2 then x1 :: sort (x2::xs)
                              else x2 :: sort (x1::xs)
let rec sort=函数
| []         -> []
|[x]->[x]

|x1::x2::xs->如果x1考虑第一个位置会出现什么结果。。。现在它只能是列表中的第一个或第二个位置(最后一个案例中的
如果

您的代码就像一个气泡循环。它将把max元素带到最后一个位置,把一些其他较大的元素带到右边

请注意,您只遍历原始列表一次。它具有线性复杂性,我们都知道仅使用比较的排序必须是O(n*logn)

如果要对该列表进行排序,可以多次重复该循环。

让rec ssort=function
let rec ssort = function
    [] -> []
    | x::xs -> 
        let min, rest =
            List.fold_left (fun (min,acc) x ->
                             if h<min then (h, min::acc)
                             else (min, h::acc))
              (x, []) xs
        in min::ssort rest
[] -> [] |x::xs-> 让敏休息一下= List.fold_左(fun(min,acc)x->
如果hIt被认为是一个基本情况tho。当列表为空时。谢谢,我现在修复了它。