From 05fbf436dcdbe9a25c17cc32d757fd7992c517d4 Mon Sep 17 00:00:00 2001 From: Jorge Gonzalez Date: Mon, 9 Oct 2017 22:48:24 -0400 Subject: [PATCH] Create mergeSort.js (#38) --- utilities/javascript/mergeSort.js | 54 +++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 utilities/javascript/mergeSort.js diff --git a/utilities/javascript/mergeSort.js b/utilities/javascript/mergeSort.js new file mode 100644 index 00000000..b5640444 --- /dev/null +++ b/utilities/javascript/mergeSort.js @@ -0,0 +1,54 @@ +function mergeSort(arr) { + if (arr.length < 2) { + // Arrays of length 0 or 1 are sorted by definition + return arr; + } + + const left = arr.slice(0, Math.floor(arr.length / 2)); + const right = arr.slice(Math.floor(arr.length / 2), Math.floor(arr.length)); + + return merge(mergeSort(left), mergeSort(right)); +} + +function merge(arr1, arr2) { + const merged = []; + + let i = 0; + let j = 0; + + while (i < arr1.length && j < arr2.length) { + if (arr1[i] < arr2[j]) { + merged.push(arr1[i]); + i++; + } else if (arr2[j] < arr1[i]) { + merged.push(arr2[j]); + j++; + } else { + merged.push(arr1[i]); + i++; + j++; + } + } + + while (i < arr1.length) { + merged.push(arr1[i]); + i++; + } + + while (j < arr2.length) { + merged.push(arr2[j]); + j++; + } + + return merged; +} + +const arr1 = [7, 2, 4, 3, 1, 2]; +const arr2 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; +const arr3 = [98322, 3242, 876, -234, 34, 12331]; +const arr4 = [1, 2, 3, 4, 5, 0]; + +console.log(mergeSort(arr1)); +console.log(mergeSort(arr2)); +console.log(mergeSort(arr3)); +console.log(mergeSort(arr4));