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 f738510

Browse files
merge sort
1 parent 9dc5caf commit f738510

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

‎book/chapters/merge-sort.adoc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
= Merge Sort
22

3-
Merge sort is an efficient sorting algorithm that uses "divide and conquer" paradigm to accomplish it task faster but it uses aditional storage in the process.
3+
Merge 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.
44

55
== Merge Sort Implementation
66

7-
Merge sort can be implemented in any programming language. This are the general steps that this sorting algorithm follows:
7+
Merge sort implementation is as follows
88

99
.Merge Sort Algorithm
1010
. 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.
@@ -20,7 +20,7 @@ Let's convert these words into code!
2020
.Merge Sort implementation in JavaScript
2121
[source, javascript]
2222
----
23-
include::{codedir}/algorithms/sorting/Merge-sort.js[tag=sort, indent=0]
23+
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
2626
<2> Start on the first element (position 0)

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function merge(array1, array2 = []) {
2+
const mergedLength = array1.length + array2.length;
3+
const mergedArray = [];
4+
5+
for (let index = 0, i1 = 0, i2 = 0; index < mergedLength; index += 1) {
6+
if (i2 >= array2.length || (i1 < array1.length && array1[i1] < array2[i2])) {
7+
mergedArray[index] = array1[i1];
8+
i1 += 1;
9+
} else if (i1 >= array1.length || (i2 < array2.length && array2[i2] < array1[i1])) {
10+
mergedArray[index] = array2[i2];
11+
i2 += 1;
12+
}
13+
}
14+
15+
return mergedArray;
16+
}
17+
18+
function splitSort(array) {
19+
const size = array.length;
20+
if (size < 2) {
21+
return array;
22+
} else if (size === 2) {
23+
return array[0] < array[1] ? array : [array[1], array[0]];
24+
}
25+
const middle = Math.ceil(size / 2);
26+
27+
return merge(
28+
splitSort(array.slice(0, middle)),
29+
splitSort(array.slice(middle)),
30+
);
31+
}
32+
33+
// tag::sort[]
34+
/**
35+
* Merge sort
36+
* Runtime: O(n log n)
37+
* @param {Array|Set} collection elements to be sorted
38+
*/
39+
function mergeSort(collection) {
40+
const array = Array.from(collection); // <1>
41+
return splitSort(array);
42+
}
43+
// end::sort[]
44+
45+
module.exports = mergeSort;

‎src/algorithms/sorting/sorting.spec.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const sortingAlgorithms = [
22
require('./selection-sort'),
33
require('./insertion-sort'),
44
require('./bubble-sort'),
5+
require('./merge-sort'),
56
];
67

78
sortingAlgorithms.forEach((sort) => {
@@ -33,5 +34,9 @@ sortingAlgorithms.forEach((sort) => {
3334
it('should sort a set', () => {
3435
expect(sort(new Set([3, 1, 2]))).toEqual([1, 2, 3]);
3536
});
37+
38+
xit('should sort with duplicated values', () => {
39+
expect(sort([1, 3, 2, 1])).toEqual([1, 1, 2, 3]);
40+
});
3641
});
3742
});

0 commit comments

Comments
(0)

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