An integer array arr is given. Rearrange all values in non-decreasing order by applying the Quick Sort algorithm.
During every partition step, one pivot value must be selected and placed at the correct sorted position. Values smaller than or equal to the pivot are moved toward the left side, and greater values are moved toward the right side. Return the sorted array.
Example 1
Input: arr = [10, 7, 8, 9, 1, 5]
Output: [1, 5, 7, 8, 9, 10]
Explanation: Pivot-based partitioning places value 5 first. Smaller values move left, greater values move right, and the same sorting process continues on both sides.
Example 2
Input: arr = [4]
Output: [4]
Explanation: A single value already occupies the correct sorted position, so no partition step is required.
Approach
A pivot is chosen to split the array into two smaller parts. Values smaller than the pivot are moved to its left, while larger values remain on its right.
A boundary index helps build the smaller-value region during the scan. After placing the pivot between the two regions, its position becomes fixed.
The same process is then repeated on the left and right parts. Each step breaks the array into smaller sections until every section has at most one element and is already sorted.
Algorithm
Start with the range
0ton - 1because it represents the complete array that needs to be sorted.Process a range only when
low < highbecause an empty or single-value range is already sorted.Choose the last value as the pivot because it gives the scan one fixed value for comparison.
Start the boundary before the range because the smaller-value region is empty at the beginning.
Scan from
lowtohigh - 1because the pivot athighshould remain unchanged during comparison.Move values no greater than the pivot into the left region so smaller values stay together.
Place the pivot just after the left region because that is its correct sorted position.
Repeat the process on both sides of the pivot because only those parts still need sorting.
Dry Run
Quick Sort
Solution
#include <bits/stdc++.h>using namespace std;class Solution {public: // Sorts an integer array with in-place Quick Sort. vector<int> quickSort(vector<int>& arr) { int n = arr.size(); solve(arr, 0, n - 1); return arr; }private: // Sorts the current index range recursively. void solve(vector<int>& arr, int low, int high) { // Empty and single-value ranges are already sorted. if (low >= high) { return; } // Split the range into smaller and greater sides using the pivot. int pivotIndex = partitionArray(arr, low, high); // Values before the pivot are sorted independently. solve(arr, low, pivotIndex - 1); // Values after the pivot are sorted independently. solve(arr, pivotIndex + 1, high); } // Places the pivot in the final sorted position. int partitionArray(vector<int>& arr, int low, int high) { int pivot = arr[high]; int smallerIndex = low - 1; // Every scanned value is compared with the pivot once. for (int current = low; current < high; current++) { // Values no greater than the pivot belong in the left region. if (arr[current] <= pivot) { smallerIndex++; swap(arr[smallerIndex], arr[current]); } } // The pivot is placed directly after the left region. swap(arr[smallerIndex + 1], arr[high]); return smallerIndex + 1; }};// Driver codeint main() { // Input array vector<int> arr = {10, 7, 8, 9, 1, 5}; // Solution object creation Solution obj; // Result printing vector<int> answer = obj.quickSort(arr); for (int value : answer) { cout << value << " "; } return 0;}Note: Direct recursion may fail for large input values. Repeated unbalanced subproblems create quadratic work, so an online judge may report Time Limit Exceeded.
Complexity Analysis
Time Complexity: O(N log N) on average because each partition scans the active range once, and balanced partitions create about log N levels. The worst-case time complexity is O(N2) when the selected pivot is repeatedly the smallest or largest value.
Space Complexity: O(log N) on average because balanced recursion keeps about log N active calls on the stack. The worst-case recursion stack space is O(N) when partitions become completely unbalanced. No extra array proportional to N is created during partitioning.
Interview follow-up Questions
No. Standard in-place Quick Sort swaps values across the array during partitioning, so equal values can change relative order.
Be the first to add a comment.