Merge Sort
Aashish Panchal
Posted on October 16, 2020
function merge(arr1, arr2){
let results = [];
let i = 0;
let j = 0;
while(i < arr1.length && j < arr2.length){
if(arr2[j] > arr1[i]){
results.push(arr1[i]);
i++;
} else {
results.push(arr2[j]);
j++;
}
}
while(i < arr1.length) {
results.push(arr1[i]);
i++;
}
while(j < arr2.length) {
results.push(arr2[j]);
j++;
}
return results;
}
function mergeSort(arr){
if(arr.length <= 1) return arr;
let mid = Math.floor(arr.length/2);
let left = mergeSort(arr.slice(0,mid));
let right = mergeSort(arr.slice(mid));
return merge(left,right);
}
mergeSort([10,24,76,73,72,1,9])
💖 💪 🙅 🚩
Aashish Panchal
Posted on October 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
opensource RoSctober Fest Wrap-Up: A Month of Code, Community, and Sloth Points! 🦥
November 13, 2024
hacktoberfest ✨ From Contributor to Core Project Maintainer: My Open Source Journey ✨
October 11, 2024
hacktoberfestchallenge My Hacktoberfest 2024 Journey: A Month of Code, Growth, and Unforgettable Lessons 🚀
November 1, 2024