Skip to content

Commit 01ea8ce

Browse files
committed
fix bubble sort docs
1 parent 9210162 commit 01ea8ce

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

book/chapters/bubble-sort.adoc

+9-16
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
= Bubble Sort
22

33
Bubble sort is a simple sorting algorithm that "bubbles up" the biggest values to the right side of the array.
4+
It's also call _((sinking sort))_ because the largest values sinks to the right side of the array.
5+
This algorithm is adaptive which means that if the array is already sorted it will take only _O(n)_ to "sort".
6+
However, if the array is quite out of order it will require _O(n^2^)_ to sort.
47

58
== Bubble Sort Implementation
69

7-
Bubble sort can be implemented in any programming language. This are the general steps that this sorting algorithm follows:
8-
9-
.Bubble Sort Algorithm
10-
. It moves one element at a time (from left to right). Everything on the left of the current element is already sorted, while everything to the right is not.
11-
. Start with the first element and make it the current element.
12-
. Compare elements to right of the current element.
13-
. Bubble up big values to the right of the array.
14-
.. Swap elements if the previous element is bigger than the previous one.
15-
. Move the current pointer to the next element and repeat for the rest of the array
16-
17-
18-
Let's convert these words into code!
10+
Bubble sort can be implemented in any programming language. Let's implement it first and explain it.
1911

2012
.Bubble Sort implementation in JavaScript
2113
[source, javascript]
2214
----
2315
include::{codedir}/algorithms/sorting/bubble-sort.js[tag=sort, indent=0]
2416
----
2517
<1> Convert any kind of iterable (array, sets, etc.) into an array or if is already and array it clones it so the input is not modified.
26-
<2> Goes through all the elements in the array
27-
<3> *Compares in pairs* (current vs next element) and swap them only if they are out of order. Bubbles up largest
28-
<4> If no swap was needed that means everything is sorted (this make this sorting adaptive, if everything is sorted it will be _O(n)_)
29-
<5> If a swap as performed means that the largest element is all the way to the right side and no need compare it anymore, thus `array.length - left`.
18+
<2> Starting from 0 compare current and next element
19+
<3> If they are out of order, swap the pair
20+
<4> Repeat pair comparison `n - 1` times, until the largest element is on the right side.
21+
<5> (optimization) If there was no swap means that the array is already sorted and no more work is needed. This make this sorting adaptive, if everything is sorted it will be only one pass: _O(n)_.
22+
<6> Each step moves the largest element from `0` to `n - i` to the right side. So, we need to do this `n - 1` to sort the array in case most elements needs to be swapped.
3023

3124
Bubble sort has a <<Quadratic>> running time, as you might infer from the nested for-loop.
3225

src/algorithms/sorting/bubble-sort.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@ const { swap } = require('./sorting-common');
99
function bubbleSort(collection) {
1010
const array = Array.from(collection); // <1>
1111

12-
let swapped = false;
12+
for (let i = 1; i < array.length; i++) { // <6>
13+
let swapped = false;
1314

14-
for (let left = 0; left < array.length; left++) { // <2>
15-
for (let right = 0; right < array.length - left; right++) { // <5>
16-
if (array[right] > array[right + 1]) { // <3>
17-
swap(array, right, right + 1);
15+
for (let current = 0; current < array.length - i; current++) { // <4>
16+
if (array[current] > array[current + 1]) { // <2>
17+
swap(array, current, current + 1); // <3>
1818
swapped = true;
1919
}
2020
}
21-
if (!swapped) break; // <4>
21+
22+
if (!swapped) break; // <5>
2223
}
2324

2425
return array;

src/algorithms/sorting/sorting.spec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ sortingAlgorithms.forEach((sort) => {
2929
expect(sort([3, 2, 1])).toEqual([1, 2, 3]);
3030
});
3131

32-
it('should sort with with already sorted array', () => {
32+
it('should sort with already sorted array', () => {
3333
expect(sort([1, 2, 3])).toEqual([1, 2, 3]);
3434
});
3535

@@ -40,5 +40,9 @@ sortingAlgorithms.forEach((sort) => {
4040
it('should sort with duplicated values', () => {
4141
expect(sort([1, 3, 2, 1])).toEqual([1, 1, 2, 3]);
4242
});
43+
44+
it('should sort with almost already sorted array', () => {
45+
expect(sort([1, 2, 3, 0])).toEqual([0, 1, 2, 3]);
46+
});
4347
});
4448
});

0 commit comments

Comments
 (0)