Skip to content

Commit ab3b6cd

Browse files
committed
updated readme to conform with changes requested
1 parent da34da5 commit ab3b6cd

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

Bubble Sort/README.markdown

+59-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Bubble Sort
22

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.
44

55
##### Runtime:
66
- Average: O(N^2)
@@ -16,6 +16,10 @@ The implementation will not be shown as the average and worst runtimes show that
1616
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.
1717
This is accomplished by looking through the array `n` times, `n` being the amount of elements in the array.
1818

19+
![animated gif of the bubble sort algorithm](https://s3.amazonaws.com/codecademy-content/programs/tdd-js/articles/BubbleSort.gif)
20+
21+
This GIF shows a inverted implementation than
22+
1923
#### Example
2024
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.
2125

@@ -52,13 +56,13 @@ This is the same for the forth and fifth passes.
5256
#### Code
5357
```swift
5458
for i in 0..<array.count {
55-
for j in 1..<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 in 1..<array.count {
60+
if array[j] < array[i] {
61+
let tmp = array[i]
62+
array[i] = array[j]
63+
array[j] = tmp
64+
}
65+
}
6266
}
6367
return array
6468
```
@@ -68,17 +72,56 @@ The bubble sort algorithm can be easily optimized by observing that the `n-th` p
6872

6973
```swift
7074
for i in 0..<array.count {
71-
for j in 1..<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 in 1..<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+
}
7882
}
7983
return array
8084
```
8185

8286
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.
8387

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]`:
89+
90+
##### First Pass
91+
[ **5 1** 4 2 8 ] -> [ **1 5** 4 2 8 ], Swaps since 5 > 1
92+
93+
[ 1 **5 4** 2 8 ] -> [ 1 **4 5** 2 8 ], Swap since 5 > 4
94+
95+
[ 1 4 **5 2** 8 ] -> [ 1 4 **2 5** 8 ], Swap since 5 > 2
96+
97+
[ 1 4 2 **5 8** ] -> [ 1 4 2 **5 8** ], Now, since these elements are already in order (8 > 5), algorithm does not swap them.
98+
99+
*by the end of the first pass, the last element is guaranteed to be the largest*
100+
101+
##### Second Pass
102+
[ **1 4** 2 5 8 ] -> [ **1 4** 2 5 8 ]
103+
104+
[ 1 **4 2** 5 8 ] -> [ 1 **2 4** 5 8 ], Swap since 4 > 2
105+
106+
[ 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*
123+
124+
##### Supporting Links
125+
[Code Pumpkin](https://codepumpkin.com/bubble-sort/)
126+
[Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort)
127+
[GeeksforGeeks](https://www.geeksforgeeks.org/bubble-sort/)

0 commit comments

Comments
 (0)