Merge sort algorithm implementation!
Intuition\nI didn\'t want to use sort function so I thought why not implement sorting algorithms for a change.\n# Approach\nBubble sort gave TLE, optimized bubb
# Intuition\nI didn\'t want to use sort function so I thought why not implement sorting algorithms for a change.\n# Approach\nBubble sort gave TLE, optimized bubble sort gave TLE, selection sort gave TLE, so ultimately went to merge sort to get the job done.\n\n\n# Code\n```javascript []\n/**\n * @param {Array} arr\n * @param {Function} fn\n * @return {Array}\n */\nvar sortBy = function(arr, fn) {\n    let n = arr.length;\n    function merge(left, right) {\n        let result = [];\n        let i = 0, j = 0;\n        while (i < left.length && j < right.length) {\n            if (fn(left[i]) <= fn(right[j])) {\n                result.push(left[i]);\n                i++;\n            } else {\n                result.push(right[j]);\n                j++;\n            }\n        }\n        while (i < left.length) {\n            result.push(left[i]);\n            i++;\n        }\n        while (j < right.length) {\n            result.push(right[j]);\n            j++;\n        }\n\n        return result;\n    }\n    for (let size = 1; size < n; size *= 2) {\n        for (let leftStart = 0; leftStart < n; leftStart += 2 * size) {\n            let mid = Math.min(leftStart + size, n);\n            let rightEnd = Math.min(leftStart + 2 * size, n);\n            let left = arr.slice(leftStart, mid);\n            let right = arr.slice(mid, rightEnd);\n            let merged = merge(left, right);\n\n            for (let i = 0; i < merged.length; i++) {\n                arr[leftStart + i] = merged[i];\n            }\n        }\n    }\n\n    return arr;\n};\n\n```
['Array', 'Merge Sort', 'JavaScript']