Quick Overview

Sort an integer array into ascending order without any built-in sort, in O(n log n) time and with as little extra space as possible. It tests whether you can implement a worst-case O(n log n) comparison sort by hand and reason about the time and space costs of different sorting algorithms.

Sort an integer array in O(n log n) time without built-in sort functions

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

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 sorting functions, in `O(n log n)` time and with the smallest space complexity possible. ### Function Signature ```python def sort_array(nums: list[int]) -> list[int]: ``` ### Rules - Return every element of `nums` in non-decreasing order. A value that appears several times in the input appears the same number of times in the output. - You may sort `nums` in place and return the same list. - Do not call a library sort or heap routine (`sorted`, `list.sort`, `heapq` or similar). ### Constraints - `1 <= len(nums) <= 5 * 10^4` - `-5 * 10^4 <= nums[i] <= 5 * 10^4` ### Examples ```text Input: nums = [5,2,3,1] Output: [1,2,3,5] ``` The four distinct values come back in ascending order. ```text Input: nums = [5,1,1,2,0,0] Output: [0,0,1,1,2,5] ``` Both copies of 0 and both copies of 1 are kept, so the output holds the same six elements as the input. ```text Input: nums = [3,-2,0,-2] Output: [-2,-2,0,3] ``` Negative values sort below zero, and the repeated -2 appears twice.

Overview: Sort an integer array into ascending order without any built-in sort, in O(n log n) time and with as little extra space as possible. It tests whether you can implement a worst-case O(n log n) comparison sort by hand and reason about the time and space costs of different sorting algorithms.

Given an array of integers `nums`, sort it in ascending order and return it. Return every element of `nums` in non-decreasing order. A value that appears several times in the input appears the same number of times in the output. You may sort `nums` in place and return the same list. Solve it without any built-in sorting function, in `O(n log n)` time and with the smallest space complexity possible. Do not call a library sort or heap routine (`sorted`, `list.sort`, `heapq`, `Array.prototype.sort`, `java.util.Arrays.sort`, `std::sort` or similar). Every value lies between -50000 and 50000, so a signed 32-bit integer (`int` in Java and C++) holds each one. ### Example 1 ```text Input: nums = [5,2,3,1] Output: [1,2,3,5] ``` The four distinct values come back in ascending order. ### Example 2 ```text Input: nums = [3,-2,0,-2] Output: [-2,-2,0,3] ``` Negative values sort below zero, and the repeated -2 appears twice. ### Constraints - `1 <= len(nums) <= 5 * 10^4` - `-5 * 10^4 <= nums[i] <= 5 * 10^4`

Constraints

  • 1 <= len(nums) <= 5 * 10^4
  • -5 * 10^4 <= nums[i] <= 5 * 10^4
  • Do not use a built-in sort or heap routine; aim for O(n log n) time and the smallest possible extra space.

Examples

Input: ([5, 2, 3, 1],)

Expected Output: [1, 2, 3, 5]

Explanation: Source example: four distinct values come back in ascending order.

Input: ([5, 1, 1, 2, 0, 0],)

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

Explanation: Source example: both copies of 0 and both copies of 1 are kept.

Hints

  1. Swapping neighbors until nothing moves takes O(n^2) comparisons on 5 * 10^4 elements; look for a method that spends about O(log n) work per element.
  2. Every copy of a repeated value must reach the output, so never skip or merge equal values.
  3. To keep extra space small, think about which O(n log n) methods can rearrange the array in place.

Sort an Integer Array Without a Library Sort

mediumGeneralSoftware Engineer

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

Return every element of nums in non-decreasing order. A value that appears several times in the input appears the same number of times in the output. You may sort nums in place and return the same list.

Solve it without any built-in sorting function, in O(n log n) time and with the smallest space complexity possible. Do not call a library sort or heap routine (sorted, list.sort, heapq, Array.prototype.sort, java.util.Arrays.sort, std::sort or similar).

Every value lies between -50000 and 50000, so a signed 32-bit integer (int in Java and C++) holds each one.

Example 1

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

The four distinct values come back in ascending order.

Example 2

Input: nums = [3,-2,0,-2]
Output: [-2,-2,0,3]

Negative values sort below zero, and the repeated -2 appears twice.

Constraints

  • 1 <= len(nums) <= 5 * 10^4
  • -5 * 10^4 <= nums[i] <= 5 * 10^4

Examples

Example 1
Input
([5, 2, 3, 1],)
Output
[1, 2, 3, 5]
Notes
Source example: four distinct values come back in ascending order.
Example 2
Input
([5, 1, 1, 2, 0, 0],)
Output
[0, 0, 1, 1, 2, 5]
Notes
Source example: both copies of 0 and both copies of 1 are kept.

Constraints

  • •1 <= len(nums) <= 5 * 10^4
  • •-5 * 10^4 <= nums[i] <= 5 * 10^4
  • •Do not use a built-in sort or heap routine; aim for O(n log n) time and the smallest possible extra space.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def sort_array(nums: list[int]) -> list[int]:
Sample call
sort_array([5, 2, 3, 1])
Language
Case 1
Input
([5, 2, 3, 1],)
Expected
[1, 2, 3, 5]
Case 2
Input
([5, 1, 1, 2, 0, 0],)
Expected
[0, 0, 1, 1, 2, 5]

Browse More Questions

Show the approach

Approach

Heap sort, done in place. First arrange the array as a max-heap, where every index i holds a value at least as large as its children at 2i+1 and 2i+2. Build it bottom-up by sifting down each internal node from index n/2 - 1 to 0: a sift swaps a node with its larger child until the node is at least as large as both children or has none. Then repeat n - 1 times: swap the root, which is the largest value in the unsorted prefix nums[0..end], with nums[end], shrink the prefix by one and sift the new root down.

Invariant: before each extraction, nums[0..end] is a max-heap and nums[end+1..n-1] holds the largest n - 1 - end values in ascending order, each no smaller than anything in the heap. Each extraction moves the largest heap value to the front of the sorted suffix and restores the heap, so when the prefix shrinks to one element the whole array is in non-decreasing order.

The algorithm only swaps elements and never copies or drops one, so every duplicate keeps its count. A single-element list skips both loops and comes back unchanged. Equal values stop a sift early because the comparison uses >=, which is still correct. All values fit in 32-bit integers and no arithmetic is done on them, so overflow cannot occur.

Building the heap costs O(n) and each of the n - 1 extractions sifts down at most log n levels, giving O(n log n) time. Only a few indices and one temporary are stored, so extra space is O(1); the C++ reference receives a const reference and returns a sorted copy of it.

Time complexity:
O(n log n)
Space complexity:
O(1) auxiliary