Input: ip[] = {10, 5, 30, 40, 2, 4, 9}
Output: op[] = {2, 4, 5, 9, 10, 30, 40}
Input: ip[] = {1, 10, 7}
Output: op[] = {1, 7, 10}
Let, input[] = {10, 5, 30, 40, 2, 4, 9}
Initialize: output[] = {}, sublist[] = {}
Move first item of input to sublist.
sublist[] = {10}
Traverse remaining items of input and if current element is greater than last item of sublist, move this item from input to sublist.
Now, sublist[] = {10, 30, 40}, input[] = {5, 2, 4, 9}
Merge sublist into output.
op = {10, 30, 40}
Next recursive call: Move first item of input to sublist. sublist[] = {5}
Traverse remaining items of input and move elements greater than last inserted.
input[] = {2, 4}
sublist[] = {5, 9}
Merge sublist into op.
output = {5, 9, 10, 30, 40}
Last Recursive Call:
{2, 4} are first moved to sublist and then merged into output.
output = {2, 4, 5, 9, 10, 30, 40}