Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 45ba64e

Browse files
fix sorting algorithms
1 parent 41a07ec commit 45ba64e

File tree

8 files changed

+53
-41
lines changed

8 files changed

+53
-41
lines changed

‎book/chapters/chapter4.adoc‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ include::sorting-intro.adoc[]
2323

2424
// Slow Sorting
2525

26-
include::insertion-sort.adoc[]
26+
include::bubble-sort.adoc[]
2727

2828
include::selection-sort.adoc[]
2929

30-
include::bubble-sort.adoc[]
30+
include::insertion-sort.adoc[]
31+
32+
33+
// include::insertion-selection-bubble-sort.adoc[]
3134

3235
// Fast Sorting
3336

‎book/chapters/insertion-sort.adoc‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ If you are given some cards that's probably how you are going to sort them.
66

77
// Good illustration on of sorting a deck of cards: https://www.khanacademy.org/computing/computer-science/algorithms/insertion-sort/a/insertion-sort
88

9-
.This is how it works:
10-
. Visit 2nd. If the current element is smaller than the previous one move to the place it should be.
11-
. Visit next element and do the same thing. The left is always sorted and the right is not (yet).
12-
. Repeat for the rest of the array.
13-
149
== Insertion Sort Implementation
1510

1611
.Insertion sort
@@ -19,11 +14,16 @@ If you are given some cards that's probably how you are going to sort them.
1914
include::{codedir}/algorithms/sorting/insertion-sort.js[tag=sort, indent=0]
2015
----
2116

22-
//.Swap function
23-
//[source, javascript]
24-
//----
25-
//include::{codedir}/algorithms/insertion-sort.js[tag=swap, indent=0]
26-
//----
17+
// .Swap function
18+
// [source, javascript]
19+
// ----
20+
// include::{codedir}/algorithms/insertion-sort.js[tag=swap, indent=0]
21+
// ----
22+
23+
.This is how it works:
24+
. Visit 2nd. If the current element is smaller than the previous one move to the place it should be.
25+
. Visit next element and do the same thing. The left side is always sorted and the right is not (yet).
26+
. Repeat for the rest of the array.
2727

2828
== Insertion Sort Properties
2929

‎book/chapters/merge-sort.adoc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ Merge sort has a _O(n log n)_ running time. For more details about the how to e
5252

5353
== Merge Sort Properties
5454

55-
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
56-
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
5755
- <<Stable>>: [big]#✅# Yes
58-
- <<In-place>>: [big]#⛔️️️️️# No, it requires auxiliary memory O(n).
59-
- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, new elements will require to sort the whole array.
60-
- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n).
56+
- <<In-place>>: [big]#️❌# No, it requires auxiliary memory O(n).
57+
- <<Online>>: [big]#️❌# No, new elements will require to sort the whole array.
58+
- <<Adaptive>>: [big]#️❌# No, mostly sorted array takes the same time O(n log n).
6159
- Recursive: Yes
60+
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
61+
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_

‎book/chapters/quick-sort.adoc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Merge sort has a _O(n log n)_ running time. For more details about the how to ex
4343

4444
== Quicksort Properties
4545

46-
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
47-
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
4846
- <<Stable>>: [big]#✅# Yes
4947
- <<In-place>>: [big]#✅# Yes
50-
- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n).
51-
- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, the pivot element can be choose at random.
48+
- <<Adaptive>>: [big]#️❌# No, mostly sorted array takes the same time O(n log n).
49+
- <<Online>>: [big]#️❌# No, the pivot element can be choose at random.
5250
- Recursive: Yes
51+
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
52+
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
5353

5454

5555
// Resources:

‎book/chapters/selection-sort.adoc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ Without destructuring assignment, swapping two values requires a temporary varia
6363

6464
== Selection Sort Properties
6565

66-
- <<Stable>>: [big]#✅# Yes
66+
// - <<Stable>>: [big]#✅# Yes
67+
- <<Stable>>: [big]#️️❌# No
6768
- <<In-place>>: [big]#✅# Yes
6869
- <<Online>>: [big]#✅# Yes
6970
- <<Adaptive>>: [big]#✅# Yes

‎src/algorithms/sorting/bubble-sort.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ const { swap } = require('./sorting-common');
22

33
// tag::sort[]
44
/**
5-
* Bubble sort
5+
* Bubble sort - Bubbles up bigger values to the right side
66
* Runtime: O(n^2)
77
* @param {Array|Set} collection elements to be sorted
88
*/
99
function bubbleSort(collection) {
1010
const array = Array.from(collection); // <1>
1111

12-
for (let outer = 0; outer < array.length; outer+=1) { // <2>
13-
for (let inner = outer + 1; inner < array.length; inner+=1) { // <3>
14-
if (array[outer] > array[inner]) { // <4>
15-
swap(array, outer,inner);
12+
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);
1616
}
1717
}
1818
}

‎src/algorithms/sorting/insertion-sort.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ const { swap } = require('./sorting-common');
22

33
// tag::sort[]
44
/**
5-
* Sorting by insertion - start from the 2nd element,
6-
* it tries to find any element (to the left) that could be bigger than the current index.
5+
* Sorting by insertion - Look for bigger numbers on the left side
6+
*
7+
* It starts from the 2nd element,
8+
* and it tries to find any element (to the left)
9+
* that could be bigger than the current index.
710
* It will move all the elements that are bigger and insert the current element where it belongs.
11+
*
812
* Runtime: O(n^2)
913
* @param {Array|Set} collection elements to be sorted
1014
*/
1115
function insertionSort(collection) {
1216
const array = Array.from(collection);
1317

14-
for (let outer = 1; outer < array.length; outer++) {
15-
const insert = array[outer];
18+
for (let right = 1; right < array.length; right++) {
19+
const insert = array[right];
1620

17-
for (let inner = outer - 1; array[inner] > insert; inner--) {
18-
swap(array, inner + 1, inner);
21+
for (let left = right - 1; array[left] > insert; left--) {
22+
swap(array, left + 1, left);
1923
}
2024
}
2125
return array;

‎src/algorithms/sorting/selection-sort.js‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,28 @@ const { swap } = require('./sorting-common');
22

33
// tag::sort[]
44
/**
5-
* Selection sort - start from the first element,
6-
* it tries to find any element (to the right) that could be smaller than the current index.
5+
* Selection sort - Look for smaller numbers on the right side
6+
*
7+
* It starts from the first element (index 0),
8+
* and tries to find any element (to the right)
9+
* that could be smaller than the current index.
710
* If it finds one, it will swap the positions.
11+
*
812
* Runtime: O(n^2)
913
* @param {Array|Set} collection elements to be sorted
1014
*/
1115
function selectionSort(collection) {
1216
const array = Array.from(collection);
1317

14-
for (let outer = 0; outer < array.length; outer+=1) {
15-
let selection = array[outer];
18+
for (let left = 0; left < array.length; left++) {
19+
let selection = array[left];
1620

17-
for (let inner = outer + 1; inner < array.length; inner+=1) {
18-
const element = array[inner];
21+
for (let right = left + 1; right < array.length; right++) {
22+
const element = array[right];
1923

2024
if (element < selection) {
21-
swap(array, outer,inner);
22-
selection = array[outer];
25+
swap(array, left,right);
26+
selection = array[left];
2327
}
2428
}
2529
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /