Skip to content

Instantly share code, notes, and snippets.

@ChenCodes
Last active January 2, 2017 03:13
Show Gist options
  • Save ChenCodes/874b825b6993c38f3428749e4546b8e6 to your computer and use it in GitHub Desktop.
Save ChenCodes/874b825b6993c38f3428749e4546b8e6 to your computer and use it in GitHub Desktop.
Bubblesort runtime is n^2 for average and worst case scenario. Memory usage is constant.
func bubbleSort(nums: inout [Int]) -> [Int] {
while true {
var swapped = false
for i in 0..<nums.count - 1 {
if nums[i] > nums[i + 1] {
let temp = nums[i]
nums[i] = nums[i + 1]
nums[i + 1] = temp
swapped = true
}
}
if !swapped {
return nums
}
}
}
bubbleSort(nums: &randomNumbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment