Skip to content

Commit 070d73c

Browse files
committed
merge sort doc
1 parent 8193614 commit 070d73c

File tree

3 files changed

+72
-18
lines changed

3 files changed

+72
-18
lines changed

book/chapters/merge-sort.adoc

+18-9
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,32 @@ Merge sort implementation is as follows
1717

1818
Let's convert these words into code!
1919

20-
.Merge Sort implementation in JavaScript
20+
.Merge Sort implementation in JavaScript (mergeSort)
2121
[source, javascript]
2222
----
2323
include::{codedir}/algorithms/sorting/merge-sort.js[tag=sort, indent=0]
2424
----
2525
<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.
26+
27+
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.
28+
29+
.Merge Sort implementation in JavaScript (splitSort)
30+
[source, javascript]
31+
----
32+
include::{codedir}/algorithms/sorting/merge-sort.js[tag=splitSort, indent=0]
33+
----
34+
<1> Recursively divide the array in half until two or less elements are left.
35+
<2> Sort two or less elements.
36+
<3> Merge back the sorted halves in ascending order.
2937

3038
Merge sort has a <<Quadratic>> running time, as you might infer from the nested for-loop.
3139

3240
== Merge Sort Properties
3341

42+
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
43+
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
3444
- <<Stable>>: [big]#✅# Yes
35-
- <<In-place>>: [big]#✅# Yes
36-
- <<Online>>: [big]#✅# Yes
37-
- <<Adaptive>>: [big]#✅# Yes
38-
- Time Complexity: [big]#⛔️# <<Quadratic>> _O(n^2^)_
39-
- Space Complexity: [big]#✅# <<Constant>> _O(1)_
45+
- <<In-place>>: [big]#⛔️️️️️# No, it requires auxiliary memory O(n).
46+
- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, new elements will require to sort the whole array.
47+
- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n).
48+
- Recursive: Yes

book/chapters/quick-sort.adoc

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
11
= Quick Sort
22

3-
Veniam veniam et reprehenderit cupidatat laboris incididunt dolore. Mollit id aliqua laboris eiusmod nostrud aliquip enim cillum ipsum consequat tempor. Proident voluptate duis nisi elit aliqua pariatur quis mollit ullamco aliqua qui.
3+
Quick sort is an efficient sorting algorithm that uses "divide and conquer" paradigm to accomplish it task faster. It uses auxiliary memory in the process of sorting.
4+
5+
== Quick Sort Implementation
6+
7+
Quick sort implementation is as follows
8+
9+
.Quick 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+
. Quick 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!
19+
20+
.Quick Sort implementation in JavaScript (QuickSort)
21+
[source, javascript]
22+
----
23+
include::{codedir}/algorithms/sorting/Quick-sort.js[tag=sort, indent=0]
24+
----
25+
<1> Convert any kind of iterable (array, sets, etc.) into an array
26+
27+
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.
28+
29+
.Quick Sort implementation in JavaScript (splitSort)
30+
[source, javascript]
31+
----
32+
include::{codedir}/algorithms/sorting/Quick-sort.js[tag=splitSort, indent=0]
33+
----
34+
<1> Recursively divide the array in half until two or less elements are left.
35+
<2> Sort two or less elements.
36+
<3> Quick back the sorted halves in ascending order.
37+
38+
Quick sort has a <<Quadratic>> running time, as you might infer from the nested for-loop.
39+
40+
== Quick Sort Properties
41+
42+
- Time Complexity: [big]#✅# <<Linearithmic>> _O(n log n)_
43+
- Space Complexity: [big]#⚠️# <<Linear>> _O(n)_
44+
- <<Stable>>: [big]#✅# Yes
45+
- <<In-place>>: [big]#⛔️️️️️# No, it requires auxiliary memory O(n).
46+
- <<Online>>: [big]#️️️️️️️⛔️️️️️# No, new elements will require to sort the whole array.
47+
- <<Adaptive>>: [big]#️️️️️️️⛔️️️️️# No, mostly sorted array takes the same time O(n log n).
48+
- Recursive: Yes

src/algorithms/sorting/merge-sort.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ function merge(array1, array2 = []) {
2424

2525
// tag::splitSort[]
2626
/**
27-
* Split array in half until two or less elements are left.
27+
* Split array in half recursively until two or less elements are left.
2828
* Sort these two elements and combine them back using the merge function.
2929
* @param {Array} array
3030
*/
3131
function splitSort(array) {
3232
const size = array.length;
33-
33+
// base case
3434
if (size < 2) {
3535
return array;
3636
} else if (size === 2) {
37-
return array[0] < array[1] ? array : [array[1], array[0]];
37+
return array[0] < array[1] ? array : [array[1], array[0]]; // <2>
3838
}
3939

40-
const middle = Math.ceil(size / 2);
41-
42-
return merge(
43-
splitSort(array.slice(0, middle)),
44-
splitSort(array.slice(middle)),
40+
// recursive split in half and merge back
41+
const half = Math.ceil(size / 2);
42+
return merge( // <3>
43+
splitSort(array.slice(0, half)), // <1>
44+
splitSort(array.slice(half)), // <1>
4545
);
4646
}
4747
// end::splitSort[]

0 commit comments

Comments
 (0)