717. Recursive Bubble Sort

EasyRecursionRecursion

Given an integer array nums, sort it in non-decreasing order using the recursive form of Bubble Sort. In one pass, adjacent elements are compared and swapped if out of order; after one pass the largest element is guaranteed to be at the end. You should then recurse on the first n-1 elements. Return the sorted array.

Input: An integer array nums.

Output: Return the sorted array as [v1,v2,...] (non-decreasing).

Examples

Example 1
Input: [5,1,4,2,3]
Output: [1,2,3,4,5]
Explanation: First pass bubbles 5 to the end; recurse on the first 4; final sorted array is [1,2,3,4,5].
Example 2
Input: [1]
Output: [1]
Explanation: A single element is already sorted.
Example 3
Input: [3,3,3]
Output: [3,3,3]
Explanation: All equal elements are already in non-decreasing order.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →