Skip to content

Commit 2e3ba39

Browse files
authored
Update Bubble Sort Docs
Corrected grammatical errors, improved wording and diction
1 parent 2fdd8b8 commit 2e3ba39

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Bubble Sort/README.markdown

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ Bubble sort is a sorting algorithm that is implemented by starting in the beginn
1111

1212
### Implementation:
1313

14-
The implementation will not be shown as the average and worst runtimes show that this is a very inefficient algorithm. However, having a grasp of the concept will help you understand the basics of simple sorting algorithms.
14+
The implementation will not be shown as the average and worst runtimes indicate that this is a very inefficient algorithm. However, grasping the concept will help you understand the basics of simple sorting algorithms.
1515

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.
16+
Bubble sort is a very simple sorting algorithm, only comparing pairs of adjacent elements in the array. If the first element is larger, it is swapped; otherwise, nothing occurs and you move onto the next comparison.
1717
This is accomplished by looking through the array `n` times, `n` being the amount of elements in the array.
1818

1919
![animated gif of the bubble sort algorithm](https://s3.amazonaws.com/codecademy-content/programs/tdd-js/articles/BubbleSort.gif)
2020

21-
This GIF shows a inverted implementation than
21+
This GIF shows an inverted implementation
2222

2323
#### Example
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.
24+
Let us take the array `[5, 1, 4, 2, 8]`, and sort the array from lowest to highest using bubble sort. For each step, the elements written in bold are being compared. In this example, three passes will be required.
2525

2626
##### First Pass
27-
[ **5 1** 4 2 8 ] -> [ **1 5** 4 2 8 ], Here, algorithm compares the first two elements, and swaps since 5 > 1.
27+
[ **5 1** 4 2 8 ] -> [ **1 5** 4 2 8 ], Here, algorithm compares the first two elements and swaps them since 5 > 1.
2828

2929
[ 1 **5 4** 2 8 ] -> [ 1 **4 5** 2 8 ], Swap since 5 > 4
3030

@@ -40,7 +40,7 @@ Let us take the array `[5, 1, 4, 2, 8]`, and sort the array from lowest number t
4040
[ 1 2 **4 5** 8 ] -> [ 1 2 **4 5** 8 ]
4141

4242
[ 1 2 4 **5 8** ] -> [ 1 2 4 **5 8** ]
43-
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
43+
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs to have one more pass without any swaps to know it is sorted.
4444

4545
##### Third Pass
4646
[ **1 2** 4 5 8 ] -> [ **1 2** 4 5 8 ]
@@ -50,8 +50,8 @@ Now, the array is already sorted, but the algorithm does not know if it is compl
5050
[ 1 2 **4 5** 8 ] -> [ 1 2 **4 5** 8 ]
5151

5252
[ 1 2 4 **5 8** ] -> [ 1 2 4 **5 8** ]
53-
54-
This is the same for the forth and fifth passes.
53+
`
54+
This is the same for the fourth and fifth passes.
5555

5656
#### Code
5757
```swift
@@ -68,7 +68,7 @@ return array
6868
```
6969

7070
#### Optimization
71-
The bubble sort algorithm can be easily optimized by observing that the `n-th` pass finds the `n-th` largest element and puts it into its final place. So, the inner loop can avoid looking at the last `n-1` items when running for the `n-th` time:
71+
Notice that the `n-th` pass finds the `n-th` largest element and puts it into its final place. With this observation, the bubble sort algorithm can be optimized by stopping the inner loop from looking at the last `n-1` items when running for the `n-th` time:
7272

7373
```swift
7474
for i in 0..<array.count {
@@ -83,7 +83,7 @@ for i in 0..<array.count {
8383
return array
8484
```
8585

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.
86+
The only change made was on the second line by adjusting the interval from `1..<array.count` to `1..<array.count - i`, effectively cutting the number of comparisons in half.
8787

8888
The ordering with the optimized code would look something like this for the array `[5, 1, 4, 2, 8]`:
8989

@@ -113,11 +113,11 @@ The ordering with the optimized code would look something like this for the arra
113113
##### Fourth Pass
114114
[ **1 2** 4 5 8 ] -> [ **1 2** 4 5 8 ]
115115

116-
There is no Fifth pass
116+
There is no fifth pass
117117

118118
#### Conclusion
119119

120-
Even with the proposed optimizations, this is still a terribly inefficient sorting algorithm. A good alternative is [Merge Sort](https://github.com/raywenderlich/swift-algorithm-club/tree/master/Merge%20Sort), that not only is better performing, has a similar degree of dificulty to implement.
120+
Even with the proposed optimizations, bubble sort remains a terribly inefficient sorting algorithm. A good alternative is [Merge Sort](https://github.com/raywenderlich/swift-algorithm-club/tree/master/Merge%20Sort), which not only performs better, but also is equally as easy to implement.
121121

122122
*Updated for the Swift Algorithm Club by Julio Brazil*
123123

0 commit comments

Comments
 (0)