Last active
November 29, 2020 19:33
-
-
Save 0xMarK/dae315b12e5b14f07fb07b4081864a92 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
func bubbleSort(_ array: [Int]) -> [Int] { | |
guard array.count > 1 else { return array } | |
var sorted = array | |
var length = array.count | |
while length > 1 { | |
for i in 1..<length { | |
if sorted[i - 1] > sorted[i] { | |
let current = sorted[i] | |
sorted[i] = sorted[i - 1] | |
sorted[i - 1] = current | |
} | |
} | |
length -= 1 | |
} | |
return sorted | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment