Algorithm


Problem Name: 912. Sort an Array

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:

  • 1 <= nums.length <= 5 * 104
  • -5 * 104 <= nums[i] <= 5 * 104

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int[] sortArray(int[] nums) {
    int n = nums.length - 1;
    mergeSort(nums, 0, n);
    return nums;
  }
  
  private void mergeSort(int[] nums, int start, int end) {
    if (end - start + 1  < = 1) {
      return;
    }
    int mid = start + (end - start) / 2;
    mergeSort(nums, start, mid);
    mergeSort(nums, mid + 1, end);
    merge(nums, start, mid, end);
  }
  
  private void merge(int[] nums, int start, int mid, int end) {
    int left = start;
    int right = mid + 1;
    int[] temp = new int[end - start + 1];
    int idx = 0;
    while (left  < = mid && right <= end) {
      if (nums[left] < nums[right]) {
        temp[idx++] = nums[left++];
      } else {
        temp[idx++] = nums[right++];
      }
    }
    while (left  < = mid) {
      temp[idx++] = nums[left++];
    }
    while (right <= end) {
      temp[idx++] = nums[right++];
    }
    for (int i = start; i  < = end; i++) {
      nums[i] = temp[i - start];
    }
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,2,3,1]

Output

x
+
cmd
[1,2,3,5]

#2 Code Example with Javascript Programming

Code - Javascript Programming


function swap(items, l, r) {
  const temp = items[l];
  items[l] = items[r];
  items[r] = temp;
}
function partition(items, start, end) {
  let pivot = items[end], s = start
  for(let i = start; i  <  end; i++) {
    if(items[i] <= pivot) {
      swap(items, s, i)
      s++
    }
  }
  swap(items, s, end)
  return s
}

function quickSort(items, left, right) {
  if(left  <  right) {
    const pIdx = partition(items, left, right)
    quickSort(items, left, pIdx - 1)
    quickSort(items, pIdx + 1, right)
  }
  return items;
}
const sortArray = function(nums) {
  return quickSort(nums, 0, nums.length - 1>;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,2,3,1]

Output

x
+
cmd
[1,2,3,5]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def sortArray(self, nums: List[int]) -> List[int]:
        if len(nums) <= 1:
            return nums

        pivot = random.choice(nums)
        lt = [v for v in nums if v < pivot]
        eq = [v for v in nums if v == pivot]
        gt = [v for v in nums if v > pivot]

        return self.sortArray(lt) + eq + self.sortArray(gt)
    
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,1,1,2,0,0]

#4 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0912_SortAnArray
    {
        private readonly Random random = new Random();

        public int[] SortArray(int[] nums)
        {
            Suffle(nums);
            Quick3WaySort(nums, 0, nums.Length - 1);
            return nums;
        }

        void Suffle(int[] nums)
        {
            var random = new Random();
            int N = nums.Length;
            for (int i = 0; i  <  N; i++)
            {
                var r = random.Next(i + 1);
                var temp = nums[r];
                nums[r] = nums[i];
                nums[i] = temp;
            }
        }

        void Quick3WaySort(int[] nums, int lo, int hi)
        {
            if (lo >= hi) return;
            int lt = lo, gt = hi;
            var i = lo;
            var v = nums[i];

            while (i  < = gt)
            {
                if (nums[i] > v)
                {
                    var temp = nums[i];
                    nums[i] = nums[gt];
                    nums[gt--] = temp;
                }
                else if (nums[i]  <  v)
                {
                    var temp = nums[i];
                    nums[i] = nums[lt];
                    nums[lt++] = temp;
                }
                else { i++; }
            }
            Quick3WaySort(nums, lo, lt - 1);
            Quick3WaySort(nums, gt + 1, hi);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [5,1,1,2,0,0]

Output

x
+
cmd
[0,0,1,1,2,5]
Advertisements

Demonstration


Previous
#911 Leetcode Online Election Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#913 Leetcode Cat and Mouse Solution in C, C++, Java, JavaScript, Python, C# Leetcode