Last active
November 11, 2017 16:49
-
-
Save volkanbicer/9eaa60de7808fee46b9544864034a9e6 to your computer and use it in GitHub Desktop.
Bubble sort for swift
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
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