Skip to content

Commit 3ca4f42

Browse files
authored
Merge pull request #1 from knaxus/merge-two-sorted-array
--update : add test file for merge-two-sorted-arrays
2 parents fb2dd2b + 461d3d6 commit 3ca4f42

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/_Problems_/merge-two-sorted-arrays/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ function mergeTwoSortedArrays(arr1, arr2) {
3838
function mergeTwoSortedArrays2(arr1, arr2) {
3939
return [...arr1, ...arr2].sort((a, b) => a - b);
4040
}
41+
42+
module.exports = {
43+
mergeTwoSortedArrays,
44+
mergeTwoSortedArrays2,
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { mergeTwoSortedArrays, mergeTwoSortedArrays2 } = require('.');
2+
3+
4+
describe('Merge Two Sorted Array which are already in ascending order ', () => {
5+
let array1 = [];
6+
let array2 = [];
7+
8+
describe('When array[i]-array[j] = 1 where i>j & j>=0', () => {
9+
beforeEach(() => {
10+
array1 = [1, 2, 3, 4, 5];
11+
array2 = [6, 7, 8];
12+
});
13+
it('Merge two sort array with complexity O(n+m)', () => {
14+
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
15+
});
16+
it('Merge two sort array with complexity O(nlogn)', () => {
17+
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
18+
});
19+
});
20+
21+
describe('When array[i]-array[j] => 1 where i>j & j>=0', () => {
22+
beforeEach(() => {
23+
array1 = [1, 3, 5, 6, 98, 100];
24+
array2 = [2, 4, 99];
25+
});
26+
it('Merge two sort array with complexity O(n+m)', () => {
27+
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 98, 99, 100]);
28+
});
29+
it('Merge two sort array with complexity O(nlogn)', () => {
30+
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 98, 99, 100]);
31+
});
32+
});
33+
34+
describe('When array[i]-array[j] <= 1 where i>j & j>=0', () => {
35+
beforeEach(() => {
36+
array1 = [1, 1, 5, 6, 98, 100];
37+
array2 = [4, 4, 99];
38+
});
39+
it('Merge two sort array with complexity O(n+m)', () => {
40+
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 1, 4, 4, 5, 6, 98, 99, 100]);
41+
});
42+
it('Merge two sort array with complexity O(nlogn)', () => {
43+
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 1, 4, 4, 5, 6, 98, 99, 100]);
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)