Skip to content

Instantly share code, notes, and snippets.

@CGA1123
Created December 1, 2015 18:34
Show Gist options
  • Save CGA1123/ec3f3d3700e8789c9080 to your computer and use it in GitHub Desktop.
Save CGA1123/ec3f3d3700e8789c9080 to your computer and use it in GitHub Desktop.
Bubble Sort implementation in smlnj
(* General function that checks if an int list is sorted *)
fun issorted [] = true |
issorted [x] = true |
issorted (x::y::t) = x <= y andalso issorted(y::t);
(* Function that does the bubbling *)
fun bubble [] = [] |
bubble [x] = [x] |
bubble (x::y::t) = if (x > y) then y::(bubble (x::t))
else x::(bubble (y::t));
(* Call bubble on list until it is sorted *)
fun bubbleSort [] = [] |
bubbleSort l = if (issorted l) then l else bubbleSort (bubble l);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment