LeetCode in Kotlin

912. Sort an Array

Medium

Given an array of integers nums, sort the array in ascending order and return it.

You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.

Example 1:

Input: nums = [5,2,3,1]

Output: [1,2,3,5]

Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).

Example 2:

Input: nums = [5,1,1,2,0,0]

Output: [0,0,1,1,2,5]

Explanation: Note that the values of nums are not necessairly unique.

Constraints:

Solution

class Solution {
    fun sortArray(nums: IntArray): IntArray {
        return mergeSort(nums, 0, nums.size - 1)
    }

    private fun mergeSort(arr: IntArray, lo: Int, hi: Int): IntArray {
        if (lo == hi) {
            val sortedArr = IntArray(1)
            sortedArr[0] = arr[lo]
            return sortedArr
        }
        val mid = (lo + hi) / 2
        val leftArray = mergeSort(arr, lo, mid)
        val rightArray = mergeSort(arr, mid + 1, hi)
        return mergeSortedArray(leftArray, rightArray)
    }

    private fun mergeSortedArray(a: IntArray, b: IntArray): IntArray {
        val ans = IntArray(a.size + b.size)
        var i = 0
        var j = 0
        var k = 0
        while (i < a.size && j < b.size) {
            if (a[i] < b[j]) {
                ans[k++] = a[i++]
            } else {
                ans[k++] = b[j++]
            }
        }
        while (i < a.size) {
            ans[k++] = a[i++]
        }
        while (j < b.size) {
            ans[k++] = b[j++]
        }
        return ans
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy