Skip to content

Commit 9210162

Browse files
committed
fix bubble sort
1 parent c95d719 commit 9210162

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

book/chapters/algorithms-intro.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ We are going to start with <<Sorting Algorithms>>
88
then you are going to learn some techniques for solving problems.
99

1010

11-
.We are going to discuss the following approaches for solving algorithms problems:
11+
.We are going to discuss the following techniques for solving algorithms problems:
1212
- <<Greedy Algorithms>>: makes greedy choices using heuristics to find the best solution without looking back.
1313
- <<Dynamic Programming>>: technique for solving problems with _overlapping subproblems_. It uses _memoization_ to avoid duplicated work.
1414
- <<Divide and Conquer>>: _divide_ problems into smaller pieces, _conquer_ each subproblem and then _join_ the results.

book/chapters/bubble-sort.adoc

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Let's convert these words into code!
2222
----
2323
include::{codedir}/algorithms/sorting/bubble-sort.js[tag=sort, indent=0]
2424
----
25-
<1> Convert any kind of iterable (array, sets, etc.) into an array
26-
<2> Start on the first element (position 0)
27-
<3> Start scanning elements that are to the right of the current element.
28-
<4> If the previous element is bigger than the previous one, then swap them. This is called bubbling up bigger values to the right.
25+
<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`.
2930

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

book/chapters/sorting-intro.adoc

+11-13
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ Sorting implementations with the same time complexity might manipulate the data
44

55
== Stable
66

7-
An stable sorting algorithms keeps the relative order of items with the same comparison criteria.
7+
An ((stable sorting)) algorithms keeps the relative order of items with the same comparison criteria.
88

9-
This specially useful when you want to sort on multiple phases. Let's say you have the following data:
9+
This specially useful when you want to sort on multiple phases.
1010

11+
.Let's say you have the following data:
1112
[source, javascript]
1213
----
1314
const users = [
@@ -18,8 +19,7 @@ const users = [
1819
];
1920
----
2021

21-
If you sort by `name` you would have
22-
22+
.If you sort by `name` you would have:
2323
[source, javascript]
2424
----
2525
[
@@ -30,10 +30,9 @@ If you sort by `name` you would have
3030
];
3131
----
3232

33-
Then, here comes the important part, if you sort by `age` you might two different results.
34-
35-
If the sorting algorithm is *stable*, it should keep the items with the same age ordered by `name`:
33+
Then, here comes the important part, if you sort by `age` you might get two different results.
3634

35+
.If the sorting algorithm is *stable*, it should keep the items with the same age ordered by `name`:
3736
[source, javascript]
3837
----
3938
[
@@ -44,8 +43,7 @@ If the sorting algorithm is *stable*, it should keep the items with the same age
4443
];
4544
----
4645

47-
However, if the sorting is *not stable*, then you will lose the relative order of the items and get something like this:
48-
46+
.However, if the sorting is *not stable*, then you will lose the relative order of the items and get something like this:
4947
[source, javascript]
5048
----
5149
[
@@ -56,18 +54,18 @@ However, if the sorting is *not stable*, then you will lose the relative order o
5654
];
5755
----
5856

59-
Both results are correct, however, having a sorting algorithm is better if you want to do multiple sorting passes.
57+
Both results are correctly sorted by `age`, however, having a stable sorting is better if you want to keep the relative possition of keys with the same value.
6058

6159
== In-place
6260

63-
An in-place sorting algorithm would a space complexity of O(1). In other words, it does not use any other auxiliary memory because it moves the items in the collection itself.
61+
An ((in-place sorting)) algorithm would a _space complexity_ of O(1). In other words, it does not use any other auxiliary memory because it moves the items in the collection itself.
6462
This is specially useful for memory constraint enviroments like robotics or embedded systems in appliances.
6563

6664
== Online
6765

6866
It can sort a list as it receives it.
69-
Online algorithms doesn't have to re-sort the whole collection for every new item added.
67+
((Online sorting)) algorithms doesn't have to re-sort the whole collection for every new item added.
7068

7169
== Adaptive
7270

73-
It runs faster an already sorted or partially sorted collection.
71+
Algorithms with ((adaptive sorting)) run faster, close to _O(n)_, on an already sorted (or partially sorted) collection.

src/algorithms/sorting/bubble-sort.js

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

12+
let swapped = false;
13+
1214
for (let left = 0; left < array.length; left++) { // <2>
13-
for (let right = left + 1; right < array.length; right++) { // <3>
14-
if (array[left] > array[right]) { // <4>
15-
swap(array, left, right);
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);
18+
swapped = true;
1619
}
1720
}
21+
if (!swapped) break; // <4>
1822
}
1923

2024
return array;
2125
}
2226
// end::sort[]
2327

24-
2528
module.exports = bubbleSort;

src/algorithms/sorting/sorting.spec.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*eslint-disable */
12
const sortingAlgorithms = [
23
require('./selection-sort'),
34
require('./insertion-sort'),
@@ -39,9 +40,5 @@ sortingAlgorithms.forEach((sort) => {
3940
it('should sort with duplicated values', () => {
4041
expect(sort([1, 3, 2, 1])).toEqual([1, 1, 2, 3]);
4142
});
42-
43-
it('should sort longer arrays', () => {
44-
45-
});
4643
});
4744
});

0 commit comments

Comments
 (0)