diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index e23a4cc2c..684b3fbce 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -34,6 +34,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with [import:7-21, lang:"golang"](code/go/bubbleSort.go) {% sample lang="racket" %} [import:5-19, lang:"racket"](code/racket/bubbleSort.rkt) +{% sample lang="swift" %} +[import:1-15, lang:"swift"](code/swift/bubblesort.swift) {% endmethod %} ... And that's it for the simplest bubble sort method. diff --git a/chapters/sorting_searching/bubble/code/swift/bubblesort.swift b/chapters/sorting_searching/bubble/code/swift/bubblesort.swift new file mode 100644 index 000000000..1c2c3818a --- /dev/null +++ b/chapters/sorting_searching/bubble/code/swift/bubblesort.swift @@ -0,0 +1,21 @@ +func bubbleSort(sortArray: inout [Int]) -> [Int] { + + for i in (1.. sortArray[j+1] { + let temp = sortArray[j] + sortArray[j] = sortArray[j + 1] + sortArray[j + 1] = temp + } + } + } + + return sortArray +} + +func main() { + var testArray = [4,5,123,759,-132,8940,24,34,-5] + print(bubbleSort(sortArray: &testArray)) +} + +main()