🚀Fast | C# & TypeScript Solution | Merge Sort ❇️✅
\u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n\n# Runtime & Memory\n\n\n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# C
# \u2B06\uFE0FLike|\uD83C\uDFAFShare|\u2B50Favourite\n\n# Runtime & Memory\n![image.png](https://assets.leetcode.com/users/images/16827e93-dc52-4c83-bab2-d44d8184796f_1677652745.368328.png)\n\n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# C# Code\n```\npublic class Solution\n{\n    public int[] SortArray(int[] nums)\n    {\n        int[] temp = new int[nums.Length];\n        MergeSort(nums, temp, 0, nums.Length - 1);\n        return nums;\n    }\n\n    private void MergeSort(int[] nums, int[] temp, int left, int right)\n    {\n        if (left < right)\n        {\n            int mid = (left + right) / 2;\n            MergeSort(nums, temp, left, mid);\n            MergeSort(nums, temp, mid + 1, right);\n            Merge(nums, temp, left, mid, right);\n        }\n    }\n\n    private void Merge(int[] nums, int[] temp, int left, int mid, int right)\n    {\n        int i = left;\n        int j = mid + 1;\n        int k = left;\n        while (i <= mid && j <= right)\n        {\n            if (nums[i] <= nums[j])\n            {\n                temp[k++] = nums[i++];\n            }\n            else\n            {\n                temp[k++] = nums[j++];\n            }\n        }\n\n        while (i <= mid)\n        {\n            temp[k++] = nums[i++];\n        }\n\n        while (j <= right)\n        {\n            temp[k++] = nums[j++];\n        }\n\n        for (int m = left; m <= right; m++)\n        {\n            nums[m] = temp[m];\n        }\n    }\n}\n```\n\n# TypeScript Code\n```\nfunction sortArray(nums: number[]): number[] {\n    let temp: number[] = new Array(nums.length);\n    mergeSort(nums, temp, 0, nums.length - 1);\n    return nums;\n};\n\nfunction mergeSort(nums: number[], temp: number[], left: number, right: number): void {\n    if (left < right) {\n        let mid: number = Math.floor((left + right) / 2);\n        mergeSort(nums, temp, left, mid);\n        mergeSort(nums, temp, mid + 1, right);\n        merge(nums, temp, left, mid, right);\n    }\n}\n\nfunction merge(nums: number[], temp: number[], left: number, mid: number, right: number): void {\n    let i: number = left;\n    let j: number = mid + 1;\n    let k: number = left;\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            temp[k++] = nums[i++];\n        }\n        else {\n            temp[k++] = nums[j++];\n        }\n    }\n\n    while (i <= mid) {\n        temp[k++] = nums[i++];\n    }\n\n    while (j <= right) {\n        temp[k++] = nums[j++];\n    }\n\n    for (let m: number = left; m <= right; m++) {\n        nums[m] = temp[m];\n    }\n}\n```
['Array', 'Divide and Conquer', 'Merge Sort', 'TypeScript', 'C#']