Created
September 4, 2019 15:53
-
-
Save bradleyyin/698682d6cc88790326ac891ebe794875 to your computer and use it in GitHub Desktop.
morning code challenge 8/21
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(_ numbers: [Int]) -> [Int] { | |
var numbers = numbers | |
var clean = false | |
while !clean { | |
clean = true | |
for i in 0...(numbers.count - 2) { | |
if numbers[i] > numbers[i + 1] { | |
let tempNum = numbers[i] | |
numbers[i] = numbers[i + 1] | |
numbers[i + 1] = tempNum | |
clean = false | |
} | |
} | |
} | |
return numbers | |
} | |
bubbleSort([1, 3, 6, 2, 4, 5]) // returns [1, 2, 3, 4, 5, 6] | |
bubbleSort([5, 8, 6, 9, 4, 1, 2]) // returns [1, 2, 4, 5, 6, 8, 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment