Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.83 KB | None | 0 0
  1. let rec quicksort list =
  2.    match list with
  3.    | [] ->                            // If the list is empty
  4.         []                            // return an empty list
  5.    | firstElem::otherElements ->      // If the list is not empty    
  6.         let smallerElements =         // extract the smaller ones    
  7.             otherElements            
  8.             |> List.filter (fun e -> e < firstElem)
  9.             |> quicksort              // and sort them
  10.         let largerElements =          // extract the large ones
  11.             otherElements
  12.             |> List.filter (fun e -> e >= firstElem)
  13.             |> quicksort              // and sort them
  14.         // Combine the 3 parts into a new list and return it
  15.         List.concat [smallerElements; [firstElem]; largerElements]
  16.  
  17. //test
  18. printfn "%A" (quicksort [1;5;23;18;9;1;3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement