You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Bubble Sort/README.markdown
+59-16
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Bubble Sort
2
2
3
-
Bubble sort is a sorting algorithm that is implemented by starting in the beginning of the array and swapping the first two elements only if the first element is greater than the second element. This comparison is then moved onto the next pair and so on and so forth. This is done until the array is completely sorted. The smaller items slowly “bubble” up to the beginning of the array.Sometimes this algorithm is refered as Sinking sort, due to the larger, or heavier elements sinking to the end of the array.
3
+
Bubble sort is a sorting algorithm that is implemented by starting in the beginning of the array and swapping the first two elements only if the first element is greater than the second element. This comparison is then moved onto the next pair and so on and so forth. This is done until the array is completely sorted. The smaller items slowly “bubble” up to the beginning of the array.Sometimes this algorithm is refered as Sinking sort, due to the larger, or heavier elements sinking to the end of the array.
4
4
5
5
##### Runtime:
6
6
- Average: O(N^2)
@@ -16,6 +16,10 @@ The implementation will not be shown as the average and worst runtimes show that
16
16
Bubble sort is a very simple sorting algorithm, it consists in comparing pairs of adjacent elements in the array, if the first element is larger, swap them, otherwise, you do nothing and go for the next comparison.
17
17
This is accomplished by looking through the array `n` times, `n` being the amount of elements in the array.
18
18
19
+

20
+
21
+
This GIF shows a inverted implementation than
22
+
19
23
#### Example
20
24
Let us take the array `[5, 1, 4, 2, 8]`, and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required.
21
25
@@ -52,13 +56,13 @@ This is the same for the forth and fifth passes.
52
56
#### Code
53
57
```swift
54
58
for i in0..<array.count {
55
-
for j in1..<array.count {
56
-
if array[j] < array[i] {
57
-
let tmp = array[i]
58
-
array[i] = array[j]
59
-
array[j] = tmp
60
-
}
61
-
}
59
+
for j in1..<array.count {
60
+
if array[j] < array[i] {
61
+
let tmp = array[i]
62
+
array[i] = array[j]
63
+
array[j] = tmp
64
+
}
65
+
}
62
66
}
63
67
return array
64
68
```
@@ -68,17 +72,56 @@ The bubble sort algorithm can be easily optimized by observing that the `n-th` p
68
72
69
73
```swift
70
74
for i in0..<array.count {
71
-
for j in1..<array.count- i {
72
-
if array[j] < array[i] {
73
-
let tmp = array[i]
74
-
array[i] = array[j]
75
-
array[j] = tmp
76
-
}
77
-
}
75
+
for j in1..<array.count- i {
76
+
if array[j] < array[i] {
77
+
let tmp = array[i]
78
+
array[i] = array[j]
79
+
array[j] = tmp
80
+
}
81
+
}
78
82
}
79
83
return array
80
84
```
81
85
82
86
The only change made was on the second line, changing the interval from `1..<array.count` to `1..<array.count - i`, effectively cutting the number of comparisons by half.
83
87
84
-
That said, this is still a terribly inefficient sorting algorithm.
88
+
The ordering with the optimized code would look something like this for the array `[5, 1, 4, 2, 8]`:
[ 1 2 **4 5** 8 ] -> [ 1 2 **4 5** 8 ], As the first loop has occured once, the inner loop stops here, not comparing 5 with 8
107
+
108
+
##### Third Pass
109
+
[**1 2** 4 5 8 ] -> [**1 2** 4 5 8 ]
110
+
111
+
[ 1 **2 4** 5 8 ] -> [ 1 **2 4** 5 8 ] again, stoping one comparison short
112
+
113
+
##### Fourth Pass
114
+
[**1 2** 4 5 8 ] -> [**1 2** 4 5 8 ]
115
+
116
+
There is no Fifth pass
117
+
118
+
#### Conclusion
119
+
120
+
Even with the proposed optimizations, this is still a terribly inefficient sorting algorithm. A good alternative is [Merge Sort](), that not only is better performing, has a similar degree of dificulty to implement.
121
+
122
+
*Updated for the Swift Algorithm Club by Julio Brazil*
0 commit comments