Last active
July 26, 2018 21:31
-
-
Save derekli66/26e3052768fcf8a4682a4f52a37de75a to your computer and use it in GitHub Desktop.
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
// Using Playground | |
func bubbleSort(_ numbers: Array<Int>) -> Array<Int> | |
{ | |
var nums = numbers | |
if (nums.count <= 1) { return nums } | |
for i in 0..<nums.count { | |
for j in 0..<(nums.count - i - 1) { | |
if (nums[j+1] < nums[j]) { | |
let temp = nums[j] | |
nums[j] = nums[j+1] | |
nums[j+1] = temp | |
} | |
} | |
} | |
return nums | |
} | |
var array = [5,4,3,2,1] | |
let sorted = bubbleSort(array) | |
print("Bubble sort result: ") | |
print(sorted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment