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 5e6ebba

Browse files
merge sort fixes
1 parent 3935fe1 commit 5e6ebba

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

‎book/chapters/bubble-sort.adoc‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(((Sorting, Sinking Sort)))
66
Bubble sort is a simple sorting algorithm that "bubbles up" the biggest values to the right side of the array.
77
It's also called _sinking sort_ because the most significant values "sink" to the right side of the array.
8-
This algorithm is adaptive which means that if the array is already sorted, it will take only _O(n)_ to "sort".
8+
This algorithm is adaptive, which means that if the array is already sorted, it will take only _O(n)_ to "sort".
99
However, if the array is entirely out of order, it will require _O(n^2^)_ to sort.
1010

1111
== Bubble Sort Implementation
@@ -19,13 +19,10 @@ include::{codedir}/algorithms/sorting/bubble-sort.js[tag=sort, indent=0]
1919
<2> Starting from index 0 compare current and next element
2020
<3> If they are out of order, swap the pair
2121
<4> Repeat pair comparison until the last element that has been bubbled up to the right side `array.length - i`.
22-
<5> (optimization) If there was no swap means that the array is already sorted and no more work is needed. This single pass makes this sorting _adaptive_ if everything is sorted it will be only one _O(n)_ operations.
22+
<5> (optimization) If there were no swaps, this means that the array is sorted. This single pass makes this sorting _adaptive_, and it will only require _O(n)_ operations.
2323
<6> Each step moves the largest element from where it was to the right side. So, we need to do this `n - 1` times to sort the array in case most elements need to be swapped.
2424

25-
26-
The `swap` function is implemented as follows.
27-
28-
.Swap function
25+
.The `swap` function is implemented as follows:
2926
[source, javascript]
3027
----
3128
include::{codedir}/algorithms/sorting/sorting-common.js[tag=swap, indent=0]
@@ -50,7 +47,7 @@ console.log(b); //️↪️ 2
5047
5148
*Swapping variables*
5249
53-
Two variables' values can be swapped in one line using destructuring expression.
50+
Two variables' values can be swapped in one line using the destructuring expression.
5451
5552
[source, javascript]
5653
----

‎book/chapters/insertion-sort.adoc‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,27 @@
22

33
(((Sorting, Insertion Sort)))
44
(((Insertion Sort)))
5-
Insertion sort is a simple sorting algorithm. It is one of the most natural way of sorting. If you are given some cards that's probably how you are going to sort them.
5+
Insertion sort is a simple sorting algorithm. It is one of the most natural ways of sorting. If I give you some cards to sort you will probably use this algorithm without knowing.
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

99
== Insertion Sort Implementation
1010

11+
Insertion sort does the following: It starts from the 2nd element, and it tries to find anything to the left that could be bigger than the current item.
12+
It will swap all the elements with higher value
13+
and insert the current element where it belongs.
14+
1115
.Insertion sort
1216
[source, javascript]
1317
----
1418
include::{codedir}/algorithms/sorting/insertion-sort.js[tag=sort, indent=0]
1519
----
16-
<1> Convert to array or clone the array.
20+
<1> Convert to an array or clone the array.
1721
<2> Start with the 2nd element. Everything on the left is considered sorted.
18-
<3> Compare current element (2nd) to the previous one. If is biggerswap them, if not move to the next one.
22+
<3> Compare current element (2nd) to the previous one. If `left - 1` is bigger, it will swap places. If not, it will continue checking the next one to the left.
1923
<4> We check every element on the left side and swap any of them that are out of order
2024

2125

22-
// .Swap function
23-
// [source, javascript]
24-
// ----
25-
// include::{codedir}/algorithms/insertion-sort.js[tag=swap, indent=0]
26-
// ----
27-
28-
// .This is how it works:
29-
// . Visit 2nd. If the current element is smaller than the previous one move to the place it should be.
30-
// . Visit next element and do the same thing. The left side is always sorted and the right is not (yet).
31-
// . Repeat for the rest of the array.
32-
3326
== Insertion Sort Properties
3427

3528
- <<Stable>>: [big]#✅# Yes

‎book/chapters/merge-sort.adoc‎

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,45 @@
11
= Merge Sort
22
(((Sorting, Merge Sort)))
33
(((Merge Sort)))
4-
Merge Sort is an efficient sorting algorithm that uses <<Divide and Conquer, divide and conquer>> paradigm to accomplish its task faster. It uses auxiliary memory in the process of sorting.
4+
Merge Sort is an efficient sorting algorithm that uses <<Divide and Conquer, divide and conquer>> paradigm to accomplish its task faster. However, It uses auxiliary memory in the process of sorting.
55

66
indexterm:[Divide and Conquer]
7-
Merge sort algorithm splits the array in halves until 2 or less elements are left. It sorts these two elements and then merge back all halves until the whole array is sorted.
7+
Merge sort algorithm splits the array into halves until 2 or fewer elements are left. It sorts these two elements and then merges back all halves until the whole collection is sorted.
88

99
image:image11.png[Mergesort visualization,width=500,height=600]
1010

1111
== Merge Sort Implementation
1212

13-
Merge sort implementation is as follows
14-
15-
.Merge Sort Algorithm
16-
. 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.
17-
. Start with the first element and make it the current element.
18-
. Compare elements to right of the current element.
19-
. Merge up big values to the right of the array.
20-
.. Swap elements if the previous element is bigger than the previous one.
21-
. Move the current pointer to the next element and repeat for the rest of the array
22-
23-
24-
Let's convert these words into code!
25-
2613
.Merge Sort implementation in JavaScript (mergeSort)
2714
[source, javascript]
2815
----
2916
include::{codedir}/algorithms/sorting/merge-sort.js[tag=sort, indent=0]
3017
----
3118
<1> Convert any kind of iterable (array, sets, etc.) into an array
3219

33-
As you can see this function is just a wrapper to transform things to array. The heavy lifting is done in `splitSort` as you can see below.
20+
As you can see this function is just a wrapper to transform things into an array. The heavy lifting is done in `splitSort` as you can see below.
3421

3522
.Merge Sort implementation in JavaScript (splitSort)
3623
[source, javascript]
3724
----
3825
include::{codedir}/algorithms/sorting/merge-sort.js[tag=splitSort, indent=0]
3926
----
40-
<1> Sort two elements manually.
27+
<1> Base case: Sort two or less items manually.
4128
<2> Recursively divide the array in half until two or less elements are left.
4229
<3> Merge back the sorted halves in ascending order.
4330

44-
Let's now take a look at the merge function:
31+
Let's take a look at the merge function:
4532

4633
.Merge Sort implementation in JavaScript (merge)
4734
[source, javascript]
4835
----
4936
include::{codedir}/algorithms/sorting/merge-sort.js[tag=merge, indent=0]
5037
----
51-
<1> We need to keep track of 3 arrays indices (mergedArray, a1 and a2).
52-
<2> If `array1` has the lowest current value, we insert it into the merged array if not we then insert `array2`.
53-
<3> End result is array1 and array2 combined in ascending order (sorted).
38+
<1> We need to keep track of 3 arrays indices: `index` which keeps track of the combined array position, `i1` which is the `array1` index and `a2` for `array2`.
39+
<2> If `array1` current element (`i1`) has the lowest value, we insert it into the `mergedArray` if not we then insert `array2` element.
40+
<3> `mergedArray` is `array1` and `array2` combined in ascending order (sorted).
5441

55-
Merge sort has a _O(n log n)_ running time. For more details about the how to extract the runtime go to <<Linearithmic>>.
42+
Merge sort has an _O(n log n)_ running time. For more details about how to extract the runtime go to <<Linearithmic>> section.
5643

5744
== Merge Sort Properties
5845

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* Merge two arrays in ascending order
44
*
5-
* @param {array} array1
6-
* @param {array} array2
5+
* @param {array} array1 sorted array 1
6+
* @param {array} array2 sorted array 2
77
* @returns {array} merged arrays in asc order
88
*
99
* @example
@@ -15,7 +15,7 @@ function merge(array1, array2 = []) {
1515

1616
// merge elements on a and b in asc order. Run-time O(a + b)
1717
for (let index = 0, i1 = 0, i2 = 0;
18-
index < mergedLength; index+=1) { // <1>
18+
index < mergedLength; index++) { // <1>
1919
if (i2 >= array2.length ||
2020
(i1 < array1.length && array1[i1] <= array2[i2])) {
2121
mergedArray[index] = array1[i1]; // <2>

0 commit comments

Comments
(0)

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