bubble_sort

Run Settings
LanguageOcaml
Language Version
Run Command
let inline sort2 f i j (a:'a array) = let i' = a.[ i ] let j' = a.[ j ] if f i' j' > 0 then a.[ i ] <- j' a.[ j ] <- i' let bubble f (xs:'a array) = if xs.Length = 0 then () let rec modified i endix = if i = endix then unmodified 0 (endix-1) else let j = i+1 sort2 f i j xs modified j endix and unmodified i endix = if i = endix then () else let j = i+1 let i' = xs.[ i ] let j' = xs.[ j ] if f i' j' > 0 then xs.[ i ] <- j' xs.[ j ] <- i' modified j endix else unmodified j endix in unmodified 0 (xs.Length-1) let bubble_imperitive f (xs:'a array) = let mutable bModified = true let mutable endix = xs.Length - 1 while bModified do bModified <- false endix <- endix - 1 for i in 0..endix do let j = i+1 let i' = xs.[ i ] let j' = xs.[ j ] if f i' j' > 0 then xs.[ i ] <- j' xs.[ j ] <- i' bModified <- true done done
Editor Settings
Theme
Key bindings
Full width
Lines