Skip to content

Commit 2164657

Browse files
Wesley-Arringtonjiegillet
authored andcommitted
Adding Bubble Sort in Swift (#180)
* Adding Bubble Sort in Swift
1 parent 3e2097b commit 2164657

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
3434
[import:7-21, lang:"golang"](code/go/bubbleSort.go)
3535
{% sample lang="racket" %}
3636
[import:5-19, lang:"racket"](code/racket/bubbleSort.rkt)
37+
{% sample lang="swift" %}
38+
[import:1-15, lang:"swift"](code/swift/bubblesort.swift)
3739
{% endmethod %}
3840

3941
... And that's it for the simplest bubble sort method.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func bubbleSort(sortArray: inout [Int]) -> [Int] {
2+
3+
for i in (1..<sortArray.count).reversed() {
4+
for j in 0..<i {
5+
if sortArray[j] > sortArray[j+1] {
6+
let temp = sortArray[j]
7+
sortArray[j] = sortArray[j + 1]
8+
sortArray[j + 1] = temp
9+
}
10+
}
11+
}
12+
13+
return sortArray
14+
}
15+
16+
func main() {
17+
var testArray = [4,5,123,759,-132,8940,24,34,-5]
18+
print(bubbleSort(sortArray: &testArray))
19+
}
20+
21+
main()

0 commit comments

Comments
 (0)