Last active
December 30, 2015 14:29
-
-
Save gofer/7842108 to your computer and use it in GitHub Desktop.
Bubble Sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(** Bubble Sort **) | |
fun bubble_sort nil = nil | |
| bubble_sort (x::xs) = | |
let | |
fun sort_aux nil = nil | |
| sort_aux (x::nil) = x::nil | |
| sort_aux (x::(y::nil)) = if x < y then x::(y::nil) else y::(x::nil) | |
| sort_aux (x::(y::xs)) = if x < y then x::(sort_aux (y::xs)) else y::(sort_aux (x::xs)) | |
fun sort_ _ nil = nil | |
| sort_ 0 (x::xs) = sort_aux (x::xs) | |
| sort_ n (x::xs) = sort_ (n-1) (sort_aux (x::xs)) | |
in | |
sort_ (length (x::xs)) (x::xs) | |
end; | |
bubble_sort [5, 1, 2, 4, 1, 3]; | |
(* val it = [1,1,2,3,4,5] : int list *) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment