Skip to content

Instantly share code, notes, and snippets.

@0xMarK
Last active November 29, 2020 19:33
Show Gist options
  • Save 0xMarK/dae315b12e5b14f07fb07b4081864a92 to your computer and use it in GitHub Desktop.
Save 0xMarK/dae315b12e5b14f07fb07b4081864a92 to your computer and use it in GitHub Desktop.
Bubble Sort
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