Advertisement
Guest User

Untitled

a guest
Jan 10th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 1.17 KB | None | 0 0
  1. // Learn more about F# at http://fsharp.net
  2. // See the 'F# Tutorial' project for more help.
  3.  
  4. let rec qsort1 = function
  5.     | h::t ->
  6.         (qsort1 (List.filter (fun x -> x <= h) t)) @ [h] @
  7.         (qsort1 (List.filter (fun x -> x > h) t))
  8.     | [] -> []
  9.  
  10. let rec qsort2 = function
  11.     | h::t ->
  12.         let l, r = (List.partition (fun x -> x <= h) t)
  13.         (qsort2 l) @ [h] @ (qsort2 r)
  14.     | [] -> []
  15.    
  16.  
  17. let rec mergesort l =
  18.     let rec merge a b out =
  19.         match (a, b) with
  20.         | ([], y) -> y::out
  21.         | (x, []) -> x::out
  22.         | (x::xs, y::ys) ->
  23.             if x <= y then merge xs b (x::out)
  24.             else merge a ys (y::out)
  25.  
  26.     let rec split l a1 a2 =
  27.         match l with
  28.         | [] -> (a1, a2)
  29.         | [x] -> (a1 @ l, a2)
  30.         | x::y::t -> split t (a1 @ [x]) (a2 @ [y])
  31.        
  32.     if List.length l < 2 then l
  33.     else
  34.         let l, r = split l [] []
  35.         List.rev (merge (mergesort l) (mergesort r) [])
  36.  
  37.  
  38.  
  39.  
  40. [<EntryPoint>]
  41. let main argv =
  42.     printfn "%A" (qsort1 [10..-1..0])
  43.     printfn "%A" (qsort2 [10..-1..0])
  44.     printfn "%A" (mergesort [10..-1..0])
  45.  
  46.     0 // return an integer exit code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement