793. Recursive Insertion Sort

Given an array of integers nums, sort the array in non-decreasing order using the recursive Insertion Sort algorithm, and return the sorted array.

  • You must implement Insertion Sort using recursion only.
  • Do not use loops (like for or while) or built-in sorting functions (sort, Arrays.sort, etc.).
  • A sorted array in non-decreasing order is an array where each element is greater than or equal to all elements that come before it.

Example 1:

Input: nums = [7, 4, 1, 5, 3]

Output: [1, 3, 4, 5, 7]

Explanation: 1 <= 3 <= 4 <= 5 <= 7.

Thus the array is sorted in non-decreasing order.

Example 2:

Input: nums = [5, 4, 4, 1, 1]

Output: [1, 1, 4, 4, 5]

Explanation: 1 <= 1 <= 4 <= 4 <= 5.

Thus the array is sorted in non-decreasing order.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [3, 2, 3, 4, 5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 1000
  • -104 <= nums[i] <= 104
  • nums[i] may contain duplicate values.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<int> insertionSort(vector<int>& nums) {
 
}
};
 
Test Case

Input:

Nums