Last active
January 2, 2017 03:13
-
-
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.
This file contains 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(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