diff --git a/src/_Problems_/merge-two-sorted-arrays/index.js b/src/_Problems_/merge-two-sorted-arrays/index.js index 5355c789..55c89f7f 100644 --- a/src/_Problems_/merge-two-sorted-arrays/index.js +++ b/src/_Problems_/merge-two-sorted-arrays/index.js @@ -38,3 +38,8 @@ function mergeTwoSortedArrays(arr1, arr2) { function mergeTwoSortedArrays2(arr1, arr2) { return [...arr1, ...arr2].sort((a, b) => a - b); } + +module.exports = { + mergeTwoSortedArrays, + mergeTwoSortedArrays2, +}; diff --git a/src/_Problems_/merge-two-sorted-arrays/merge-two-sorted-arrays.test.js b/src/_Problems_/merge-two-sorted-arrays/merge-two-sorted-arrays.test.js new file mode 100644 index 00000000..5a3d4cc7 --- /dev/null +++ b/src/_Problems_/merge-two-sorted-arrays/merge-two-sorted-arrays.test.js @@ -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]); + }); + }); +});