Advertisement

Sort an Array - LeetCode 912 Solution

Sort an Array - Complete Solution Guide

Sort an Array is LeetCode problem 912, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] O

Detailed Explanation

The problem requires sorting an array of integers in ascending order. The constraint is to achieve this in O(n log n) time complexity without using built-in sorting functions. The input is an array `nums` of integers, and the output should be the sorted array. The length of the array and the range of numbers within the array are also specified in the constraints.

Solution Approach

The provided solution utilizes the Heap Sort algorithm to sort the array. Heap sort involves two main phases: building a max heap from the input array and then repeatedly extracting the maximum element from the heap and placing it at the end of the array. This process effectively sorts the array in ascending order because each time we extract the maximum element, we are left with a smaller heap, and the extracted element is placed at its correct position in the sorted array.

Step-by-Step Algorithm

  1. Step 1: Build a max heap from the input array. This is done by iterating through the array from the middle towards the beginning and calling the `heapify` function on each element.
  2. Step 2: The `heapify` function ensures that the subtree rooted at a given index satisfies the max heap property (the parent is greater than or equal to its children).
  3. Step 3: After building the max heap, repeatedly extract the maximum element (which is at the root of the heap) and swap it with the last element of the unsorted portion of the array.
  4. Step 4: Reduce the size of the heap by one and call `heapify` on the root to maintain the max heap property.
  5. Step 5: Repeat steps 3 and 4 until the entire array is sorted.

Key Insights

  • Insight 1: The problem asks for O(n log n) sorting, which eliminates options like bubble sort, insertion sort, and selection sort which are O(n^2).
  • Insight 2: Heap sort can be implemented in-place, satisfying the requirement of smallest space complexity.
  • Insight 3: Understanding the heapify process is crucial for building and maintaining the heap structure.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Divide and Conquer, Sorting, Heap (Priority Queue), Merge Sort, Bucket Sort, Radix Sort, Counting Sort.

Companies

Asked at: Hive, Infosys.