In this program merged two array and then sorted using temp.but this not correct method.because two array are sorted ,so method should be unique i.e. merging of two sorted i
function mergeSortedArray(a, b) {
var arr = a.concat(b).sort(function(a, b) {
return a - b;
});
return arr;
}
console.log(mergeSortedArray(a, b));
function merge(arr1, arr2) {
const arr = [];
while (arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
arr.push(arr1.shift());
} else {
arr.push(arr2.shift());
}
}
return [...arr, ...arr1, ...arr2];
}
* If you want to merge the arrays in-place, clone each array and use it in the while loop instead.
Example: clonedArr1 = [...arr1];
If you don't care about performance of the sort operation, you can use Array.sort:
var a=[1,2,3,5,9];
var b=[4,6,7,8];
var c = a.concat(b).sort((a,b)=>a > b);
console.log(c)
Of course, using the knowledge that the two arrays are already sorted can reduce runtime.
function mergeArrays(arr1, arr2) {
if (!arePopulatedArrays(arr1, arr2))
return getInvalidArraysResult(arr1, arr2);
let arr1Index = 0;
let arr2Index = 0;
const totalItems = arr1.length + arr2.length;
const mergedArray = new Array(totalItems);
for (let i = 0; i < totalItems; i++) {
if (hasItems(arr1, arr1Index)) {
if (hasItems(arr2, arr2Index)) {
if (HasSmallestItem(arr1, arr2, arr1Index, arr2Index)) {
mergedArray[i] = arr1[arr1Index++];
} else {
mergedArray[i] = arr2[arr2Index++];
}
} else {
mergedArray[i] = arr1[arr1Index++];
}
} else {
mergedArray[i] = arr2[arr2Index++];
}
}
return mergedArray;
}
function arePopulatedArrays(arr1, arr2) {
if (!arr1 || arr1.length === 0)
return false;
if (!arr2 || arr2.length === 0)
return false;
return true;
}
function getInvalidArraysResult(arr1, arr2) {
if (!arr1 && !arr2)
return [];
if ((!arr2 || arr2.length === 0) && (arr1 && arr1.length !== 0))
return arr1;
if ((!arr1 || arr1.length === 0) && (arr2 && arr2.length !== 0))
return arr2;
return [];
}
function hasItems(arr, index) {
return index < arr.length;
}
function HasSmallestItem(arr1, arr2, arr1Index, arr2Index) {
return arr1[arr1Index] <= arr2[arr2Index];
}
let Array1 = [10,20,30,40];
let Array2 = [15,25,35];
let mergeArray=(arr1, arr2)=>{
for(var i = arr2.length-1; i>= 0; i--){
let curr1 = arr2[i]
for(var j = arr1.length-1; j>= 0; j--){
let curr2 = arr1[j]
if(curr1<curr2){
arr1[j+1] = curr2
}else{
arr1[j+1] = curr1
break;
}
}
}
return arr1
}
mergeArray(Array1, Array2)
I have been working on it for while now, and I have found a good solution. I didn't came up with this algorithm, but I implemented it properly in Javscript. I have tested it with massive number of array, and I have included comments so it's easier to understand. Unlike many other solutions, this one of the most efficient one, and I have included some tests. You run the code to verify that works. Time Complexity of a this solution is O(n)
function mergeTwoSortedArraay(rightArr, leftArr) {
// I decided to call frist array "rightArr" and second array "leftArr"
var mergedAr = [], sizeOfMergedArray = rightArr.length + leftArr.length; // so if rightArray has 3 elements and leftArr has 4, mergedArray will have 7.
var r = 0, l =0; // r is counter of rightArr and l is for leftArr;
for(var i =0; i< sizeOfMergedArray; ++i) {
if(rightArr[r] >= leftArr[l] || r >= rightArr.length) { // r >= rightArr.length when r is equal to greater than length of array, if that happens we just copy the reaming
// items of leftArr to mergedArr
mergedAr[i] = leftArr[l];
l++;
} else {
mergedAr[i] = rightArr[r];
r++;
}
}
return mergedAr;
}
// few tests
console.log(mergeTwoSortedArraay([ 0, 3, 4, 7, 8, 9 ],[ 0, 4, 5, 6, 9 ]));
console.log(mergeTwoSortedArraay([ 7, 13, 14, 51, 79 ],[ -356, 999 ]));
console.log(mergeTwoSortedArraay([ 7, 23, 64, 77 ],[ 18, 42, 45, 90 ]));