Skip to content

Instantly share code, notes, and snippets.

@volkanbicer
Last active November 11, 2017 16:49
Show Gist options
  • Save volkanbicer/9eaa60de7808fee46b9544864034a9e6 to your computer and use it in GitHub Desktop.
Save volkanbicer/9eaa60de7808fee46b9544864034a9e6 to your computer and use it in GitHub Desktop.
Bubble sort for swift
import Foundation
func bubbleSort(_ list: [Int]) -> [Int]{
var array = list
var swapped = true
while swapped{
swapped = false
for i in 0..<array.count-1{
if array[i] > array[i+1]{
(array[i], array[i+1]) = (array[i+1], array[i])
swapped = true
}
}
}
return array
}
func bubleSort(_ array: [Int]) -> [Int]{
var a = array
for i in 0..<a.count-1{
for j in i+1..<a.count{
if a[i] > a[j]{
(a[i], a[j]) = (a[j], a[i])
}
}
}
return a
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment