Skip to content

Instantly share code, notes, and snippets.

@derekli66
Last active July 26, 2018 21:31
Show Gist options
  • Save derekli66/26e3052768fcf8a4682a4f52a37de75a to your computer and use it in GitHub Desktop.
Save derekli66/26e3052768fcf8a4682a4f52a37de75a to your computer and use it in GitHub Desktop.
// 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