Advertisement
xavierm02

Untitled

Nov 7th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.77 KB | None | 0 0
  1. let partition pivot l =
  2.     let rec loop l l1 l2 =
  3.         match l with
  4.             | [ ] -> l1, l2
  5.             | head :: tail ->
  6.                 if head < pivot then
  7.                     loop tail ( head :: l1 ) l2
  8.                 else
  9.                     loop tail l1 ( head :: l2 )
  10.     in
  11.     loop l [ ] [ ]
  12. ;;
  13.  
  14. let rec quicksort = function
  15.     | [ ] -> [ ]
  16.     | pivot :: l ->
  17.         let l1, l2 = partition pivot l in
  18.         ( quicksort l1 ) @ ( pivot :: ( quicksort l2 ) )
  19. ;;
  20.  
  21. let rec print_list l =
  22.     match l with
  23.         | [ ] -> ( )
  24.         | head :: tail -> begin
  25.             print_int head;
  26.             print_newline ( );
  27.             print_list tail
  28.         end
  29. ;;
  30.  
  31. let l = [ 2;3;2; 32;3464;234;234;5;45;63;2 ] in
  32. print_list ( quicksort l );;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement