Created
December 1, 2015 18:34
-
-
Save CGA1123/ec3f3d3700e8789c9080 to your computer and use it in GitHub Desktop.
Bubble Sort implementation in smlnj
This file contains 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
(* 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