Problem Description
Given an array of integers, the goal is to sort the array in ascending order without using any built-in sort functions. The solution must run in O(nlog(n)) time and use as little extra space as possible.
Key Insights
- Must not use built-in functions.
- Achieve O(nlog(n)) worst-case time complexity.
- Aim for minimum additional space; using an in-place algorithm is ideal.
- Heap sort is a good candidate because it offers O(nlog(n)) time with O(1) additional space.
Space and Time Complexity
Time Complexity: O(nlog(n)) Space Complexity: O(1) (in-place)
Solution
The chosen approach is Heap Sort. Heap sort works by first building a max heap from the input array. A max heap is a complete binary tree where the value in each node is greater than or equal to the values in its children. After the max heap is constructed, the largest element is at the root of the heap. Then, we repeatedly swap the root of the heap with the last element of the heap and reduce the heap size by one. We then re-heapify the root to restore the max heap property. This process continues until the heap is empty and the array is sorted in ascending order.
Key steps in the algorithm:
- Build the max heap in-place by calling a helper function (heapify) for each non-leaf node.
- Iteratively swap the first element (largest in the heap) with the last element and reduce the heap size.
- Re-heapify the array from the root index to ensure the max heap property is maintained.
This approach avoids the extra space required for algorithms like merge sort and guarantees O(nlog(n)) worst-case time.
Code Solutions
Below are the implementations in Python, JavaScript, C++, and Java with line-by-line comments.