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 8990522

Browse files
Add merge sort in es6
1 parent 28b93ad commit 8990522

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// sample of arrays to sort
2+
const arrayRandom = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
3+
const arrayOrdered = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
4+
const arrayReversed = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
5+
6+
let countOuter = 0;
7+
let countInner = 0;
8+
let countSwap = 0;
9+
10+
function resetCounters() {
11+
countOuter = 0;
12+
countInner = 0;
13+
countSwap = 0;
14+
}
15+
16+
// top-down implementation
17+
function mergeSortTopDown(array) {
18+
countOuter++;
19+
if(array.length < 2) {
20+
return array;
21+
}
22+
23+
const middle = Math.floor(array.length / 2);
24+
const left = array.slice(0, middle);
25+
const right = array.slice(middle);
26+
27+
return mergeTopDown(mergeSortTopDown(left), mergeSortTopDown(right));
28+
}
29+
30+
function mergeTopDown(left, right) {
31+
const array = [];
32+
33+
while(left.length && right.length) {
34+
countInner++;
35+
if(left[0] < right[0]) {
36+
array.push(left.shift());
37+
} else {
38+
array.push(right.shift());
39+
}
40+
}
41+
return array.concat(left.slice()).concat(right.slice());
42+
}
43+
44+
mergeSortTopDown(arrayRandom.slice()); // => outer: 19 inner: 24 swap: 0
45+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
46+
resetCounters();
47+
48+
mergeSortTopDown(arrayOrdered.slice()); // => outer: 19 inner: 15 swap: 0
49+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
50+
resetCounters();
51+
52+
mergeSortTopDown(arrayReversed.slice()); // => outer: 19 inner: 19 swap: 0
53+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
54+
resetCounters();
55+
56+
// bottom-up implementation
57+
function mergeSortBottomUp(array) {
58+
let step = 1;
59+
while (step < array.length) {
60+
countOuter++;
61+
let left = 0;
62+
while (left + step < array.length) {
63+
countInner++;
64+
mergeBottomUp(array, left, step);
65+
left += step * 2;
66+
}
67+
step *= 2;
68+
}
69+
return array;
70+
}
71+
function mergeBottomUp(array, left, step) {
72+
const right = left + step;
73+
const end = Math.min(left + step * 2 - 1, array.length - 1);
74+
let leftMoving = left;
75+
let rightMoving = right;
76+
const temp = [];
77+
78+
for (let i = left; i <= end; i++) {
79+
if ((array[leftMoving] <= array[rightMoving] || rightMoving > end) &&
80+
leftMoving < right) {
81+
temp[i] = array[leftMoving];
82+
leftMoving++;
83+
} else {
84+
temp[i] = array[rightMoving];
85+
rightMoving++;
86+
}
87+
}
88+
89+
for (let j = left; j <= end; j++) {
90+
countSwap++;
91+
array[j] = temp[j];
92+
}
93+
}
94+
95+
mergeSortBottomUp(arrayRandom.slice()); // => outer: 4 inner: 9 swap: 36
96+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
97+
resetCounters();
98+
99+
mergeSortBottomUp(arrayOrdered.slice()); // => outer: 4 inner: 9 swap: 36
100+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
101+
resetCounters();
102+
103+
mergeSortBottomUp(arrayReversed.slice()); // => outer: 4 inner: 9 swap: 36
104+
console.log('outer:', countOuter, 'inner:', countInner, 'swap:', countSwap);
105+
resetCounters();
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// array to sort
2+
const array = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
3+
4+
// top-down implementation
5+
function mergeSortTopDown(array) {
6+
if(array.length < 2) {
7+
return array;
8+
}
9+
10+
const middle = Math.floor(array.length / 2);
11+
const left = array.slice(0, middle);
12+
const right = array.slice(middle);
13+
14+
return mergeTopDown(mergeSortTopDown(left), mergeSortTopDown(right));
15+
}
16+
function mergeTopDown(left, right) {
17+
const array = [];
18+
19+
while(left.length && right.length) {
20+
if(left[0] < right[0]) {
21+
array.push(left.shift());
22+
} else {
23+
array.push(right.shift());
24+
}
25+
}
26+
return array.concat(left.slice()).concat(right.slice());
27+
}
28+
29+
console.log(mergeSortTopDown(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
30+
31+
// bottom-up implementation
32+
function mergeSortBottomUp(array) {
33+
let step = 1;
34+
while (step < array.length) {
35+
let left = 0;
36+
while (left + step < array.length) {
37+
mergeBottomUp(array, left, step);
38+
left += step * 2;
39+
}
40+
step *= 2;
41+
}
42+
return array;
43+
}
44+
function mergeBottomUp(array, left, step) {
45+
const right = left + step;
46+
const end = Math.min(left + step * 2 - 1, array.length - 1);
47+
let leftMoving = left;
48+
let rightMoving = right;
49+
const temp = [];
50+
51+
for (let i = left; i <= end; i++) {
52+
if ((array[leftMoving] <= array[rightMoving] || rightMoving > end) &&
53+
leftMoving < right) {
54+
temp[i] = array[leftMoving];
55+
leftMoving++;
56+
} else {
57+
temp[i] = array[rightMoving];
58+
rightMoving++;
59+
}
60+
}
61+
62+
for (let j = left; j <= end; j++) {
63+
array[j] = temp[j];
64+
}
65+
}
66+
67+
console.log(mergeSortBottomUp(array.slice())); // => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

0 commit comments

Comments
(0)

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