js All Sorting Algorithms | Quick, Merge, Bucket ,Tree, heap and others...
Merge Sort : O(n log n) time O(n) space \n\n\nconst merge = (left, right) => {\n    const mergeArr = [];\n    let i=0,j=0;\n    while(i < left.length && j < rig
## Merge Sort : O(n log n) time O(n) space \n\n```\nconst merge = (left, right) => {\n    const mergeArr = [];\n    let i=0,j=0;\n    while(i < left.length && j < right.length) {\n        if (left[i] <right[j]) {\n            mergeArr.push(left[i++]);\n        } else {\n            mergeArr.push(right[j++]);\n        }\n    }\n    while (i < left.length) {\n        mergeArr.push(left[i++]);\n    }\n    while (j < right.length) {\n        mergeArr.push(right[j++]);\n    }\n    \n    return mergeArr;\n}\nconst mergeSort = (nums, start,end) => {\n    if (end == start) {\n        return [nums[start]];\n    }\n    const mid   = start + Math.floor((end-start)/2);\n    const left  = mergeSort(nums, start, mid);\n    const right = mergeSort(nums, mid+1, end);\n    const mergeArr = merge(left, right);\n\n    return mergeArr;\n}\nvar sortArray = function(nums) {\n    return mergeSort(nums, 0, nums.length-1);\n};\n/*\n* Runtime: 140 ms, faster than 73.51% of JavaScript online submissions for Sort an Array.\n* Memory Usage: 54.4 MB, less than 37.99% of JavaScript online submissions for Sort an Array.\n*/\n```\n\n\n\n## Quick Sort :O(n log n) time O(1) space | worst case O(n^2)\n\n```\nconst swap = (nums, start,end) => {\n    [nums[start],nums[end]] = [nums[end],nums[start]];\n}\nconst partition = (nums, start, end, pivot) => {\n    let left = start, right=end;\n    while(left <= right) {\n        while(nums[left] < pivot && left <= end) {\n            left++;\n        }\n        while(left <=right && nums[right] > pivot && right >= start) {\n            right--;\n        }\n        if (left <= right) {\n            swap(nums, left, right);\n            left++;\n            right--;\n        }\n    }\n    \n    return left;\n}\nconst quickSort = (nums, start, end) => {\n    if (start >= end) return ;\n    const mid = start + Math.floor((end-start)/2);\n    const idx = partition(nums, start, end, nums[mid]);\n    quickSort(nums, start, idx-1);\n    quickSort(nums, idx, end); \n}\n\nvar sortArray = function(nums) {\n    quickSort(nums, 0, nums.length-1);\n    return nums;\n    \n};\n/*\n*\n* Runtime: 124 ms, faster than 96.80% of JavaScript online submissions for Sort an Array.\n* Memory Usage: 50.2 MB, less than 70.74% of JavaScript online submissions for Sort an Array.\n*\n*/\n\n```\n## Heap Sort : O(n log n) time O(1) space\n```\nconst heapify = (nums, n) => {\n    const parent = Math.floor((n-1)/2);\n    if (parent < 0) {\n        return;\n    }\n    if (nums[parent] > nums[n]) {\n        return;\n    }\n    [nums[parent],nums[n]] = [nums[n],nums[parent]];\n    return heapify(nums, parent)\n}\n\nconst updateHeap = (nums,n, lastIdx) => {\n    if (n > lastIdx) return;\n    const [left,right] = [2*n+1, 2*n+2];\n    \n    if (left > lastIdx) {\n        return;\n    }\n    let next = left;\n    if (right <= lastIdx && nums[left] < nums[right] ) {\n        next = right\n    }\n    if (nums[next] > nums[n]) {\n        [nums[n], nums[next]] = [nums[next],nums[n]];\n        updateHeap(nums, next, lastIdx);\n    } \n\n}\nconst sortArray = function(nums) {\n    for(let i=1; i<nums.length;i++) {\n        heapify(nums, i);\n    }\n    for(let i=nums.length-1;i >0;i--) {\n        const max = nums[0];\n        nums[0] = nums[i];\n        updateHeap(nums, 0, i-1);\n        nums[i] = max;\n    }\n    return nums;\n};\n\n/*\n*\n*Runtime: 168 ms, faster than 52.18% of JavaScript online submissions for Sort an Array.\n*Memory Usage: 49.9 MB, less than 72.85% of JavaScript online submissions for Sort an Array.\n*\n*/\n```\n# Bucket Sort : 0(d*n) t.c O(n) s.c\n`d = number of digits in max number `\n\n```\nconst bucketSort = nums => {\n    let place = 1, max = -Infinity;\n    for(const num of nums) {\n        max = Math.max(max, num);\n    }\n    const dcount = String(max).length;\n    for(let i =0 ;i< dcount; i++) {\n        const digits = Array.from(new Array(10), () => []);\n        for(const num of nums) {\n            const d =  Math.floor(num/place) % 10;\n            digits[d].push(num);\n        }\n        let idx  = 0;\n        for(const digit  of digits) {\n            for(const num of digit) {\n                nums[idx++] = num;\n            }\n        }\n        place *= 10;  \n    }\n} \n\nvar sortArray = function(nums) {\n    const p = [];\n    const n = [];\n    for(const num of nums) {\n        const arr = num < 0 ? n : p;\n        arr.push(Math.abs(num));\n    }\n    bucketSort(p);\n    bucketSort(n);\n    let idx =0;\n    for(let i=n.length-1;i >= 0 ;i--) {\n        nums[idx++] = -n[i];\n    }\n    for(const pnum of p) {\n        nums[idx++] = pnum;\n    }\n    \n    return nums;\n};\n\n```\n## Tree Sort : O(n log n) time space O(n) space \nWorst case O(n^2)(unbalanced tree) \nFor balanced tree worst O(n log n) time \n```\nclass Tree {\n    constructor(val, left=null, right=null) {\n        this.val = val;\n        this.count = 1;\n        this.left = left;\n        this.right = right;\n    }\n}\n\nconst find = (root, val) => {\n    let node = root;\n    while(node !== null) {\n        if (node.val == val) {\n            return node;\n        }\n        if (node.val > val) {\n            if (!node.left) {\n                return node;\n            }\n            node = node.left;\n        } else {\n            if (!node.right) {\n                return node;\n            }\n            node = node.right;\n        }\n\n    }\n}\nconst insert = (root, val) => {\n    const node  = find(root, val);\n    if (node.val == val) {\n        node.count += 1;\n        return;\n\n    }\n    if (node.val > val) {\n        node.left = new Tree(val);\n    } else {\n        node.right = new Tree(val);\n    }\n}\nconst inorder = (root, nums,idx) => {\n    if (!root) return ;\n    inorder(root.left, nums, idx);\n    const val = root.val;\n    for(let i=0;i<root.count;i++) {\n        nums[idx.n] = val;\n        idx.n += 1;\n    }\n  \n   inorder(root.right,nums,idx);\n}\nvar sortArray = function(nums) {\n    const root = new Tree(nums[0]);\n    for(let i=1;i<nums.length;i++) {\n        insert(root, nums[i]);\n    }\n    const idx = {n:0};\n    inorder(root, nums, idx);\n    \n    return nums;\n}\n\n/*\nThough Tree sort passes test but is slow, blanced tree will be faster. \nRuntime: 7356 ms, faster than 5.01% of JavaScript online submissions for Sort an Array.\nMemory Usage: 59.1 MB, less than 8.95% of JavaScript online submissions for Sort an Array.\n*/\n```\n\n## Insertion Sort : O(n^2) time O(1) space | best O(n) time [TLE]\n\n```\nvar sortArray = function(nums) { \n    for(let i=1;i<nums.length;i++) {\n        if(nums[i-1] > nums[i]) {\n            let j=i;\n            while(j>=0 && nums[j-1] > nums[j]) {\n                [nums[j-1], nums[j]] = [nums[j], nums[j-1]];\n                j--;\n            }\n        }\n    }\n    return nums;\n}\n```\n\n## Bubble Sort : O(n^2) time [TLE]\n\n```\nvar sortArray = function(nums) {\n    for(let i=0;i<nums.length;i++) {\n        for(let j=0;j<nums.length;j++) {\n            if (j <nums.length-1 && nums[j] > nums[j+1]) {\n                [nums[j],nums[j+1]] = [nums[j+1], nums[j]];\n            }\n        }\n    }\n    \n    return nums;\n};\n```\n\n\nI will add more if i feel bored and feel like coding a Sorting algorithm. \uD83D\uDE05 \uD83E\uDD20
['Sorting', 'Heap (Priority Queue)', 'Merge Sort', 'JavaScript']