Skip to content

Commit

Permalink
Merge pull request mre#68 from jasondown/bubble_sort_fsharp
Browse files Browse the repository at this point in the history
Added F# bubble sort solution.
  • Loading branch information
mre authored Oct 13, 2018
2 parents f3a2234 + 72b9715 commit ffbec5d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions problems/bubble-sort/bubbleSort.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let swap i j (arr : 'a array) =
let temp = arr.[i]
arr.[i] <- arr.[j]
arr.[j] <- temp

let bubbleSort (arr : 'a array) =
let rec sort (arr : 'a array) =
let mutable swapCount = 0
for i = 0 to arr.Length - 2 do
if arr.[i] > arr.[i + 1] then
swap i (i + 1) arr
swapCount <- swapCount + 1

if swapCount > 0
then sort arr
else arr
sort arr

// Test
// prints: -10 0 1 2 3 4 5 50 100
[|5;3;1;2;4;100;0;50;-10|] |> bubbleSort |> Array.iter (printf "%i ")

0 comments on commit ffbec5d

Please sign in to comment.