1// Merge Sort (Recursive)
2function mergeSort (unsortedArray) {
3 // No need to sort the array if the array only has one element or empty
4 if (unsortedArray.length <= 1) {
5 return unsortedArray;
6 }
7 // In order to divide the array in half, we need to figure out the middle
8 const middle = Math.floor(unsortedArray.length / 2);
9
10 // This is where we will be dividing the array into left and right
11 const left = unsortedArray.slice(0, middle);
12 const right = unsortedArray.slice(middle);
13
14 // Using recursion to combine the left and right
15 return merge(
16 mergeSort(left), mergeSort(right)
17 );
18}