Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/_Problems_/merge-two-sorted-arrays/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ function mergeTwoSortedArrays(arr1, arr2) {
function mergeTwoSortedArrays2(arr1, arr2) {
return [...arr1, ...arr2].sort((a, b) => a - b);
}

module.exports = {
mergeTwoSortedArrays,
mergeTwoSortedArrays2,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { mergeTwoSortedArrays, mergeTwoSortedArrays2 } = require('.');


describe('Merge Two Sorted Array which are already in ascending order ', () => {
let array1 = [];
let array2 = [];

describe('When array[i]-array[j] = 1 where i>j & j>=0', () => {
beforeEach(() => {
array1 = [1, 2, 3, 4, 5];
array2 = [6, 7, 8];
});
it('Merge two sort array with complexity O(n+m)', () => {
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
it('Merge two sort array with complexity O(nlogn)', () => {
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]);
});
});

describe('When array[i]-array[j] => 1 where i>j & j>=0', () => {
beforeEach(() => {
array1 = [1, 3, 5, 6, 98, 100];
array2 = [2, 4, 99];
});
it('Merge two sort array with complexity O(n+m)', () => {
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 98, 99, 100]);
});
it('Merge two sort array with complexity O(nlogn)', () => {
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 2, 3, 4, 5, 6, 98, 99, 100]);
});
});

describe('When array[i]-array[j] <= 1 where i>j & j>=0', () => {
beforeEach(() => {
array1 = [1, 1, 5, 6, 98, 100];
array2 = [4, 4, 99];
});
it('Merge two sort array with complexity O(n+m)', () => {
expect(mergeTwoSortedArrays(array1, array2)).toEqual([1, 1, 4, 4, 5, 6, 98, 99, 100]);
});
it('Merge two sort array with complexity O(nlogn)', () => {
expect(mergeTwoSortedArrays2(array1, array2)).toEqual([1, 1, 4, 4, 5, 6, 98, 99, 100]);
});
});
});