

You are given an array consisting of N integers. Your task is to sort the array in non-descending order using merge sort. Perform the task as in-place.
Note:In-place means that your implementation does not require extra memory for merge operation, unlike the standard implementation.
Example:
Let the array be [1, 2, -3, 4, -4, -5]. On sorting the array in non-descending order we get the resulting array [-5, -4, -3, 1, 2, 4].
The very first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of every test case contains an integer ‘N’ denoting the number of elements present in the array.
The second line of every test case contains ‘N’ space-separated integers denoting the elements present in the array.
Output format:
For each test case, the resulting sorted array is printed.
Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just return the sorted array.
1 <= T <= 10
1 <= N <= 5000
-10^5 <= Arr[i] <= 10^5
Where ‘T’ represents the number of test cases and ‘N’ represents the number of elements present in the array.
Time Limit: 1 sec
2
5
1 -4 -2 5 3
2
2 1
-4 -2 1 3 5
1 2
For the first test case we have, array: [1, -4, -2, 5, 3] and N = 5.
On sorting the array in non-descending order we get the resulting array [-4, -2, 1, 3, 5].
For the second test case we have, array: [2, 1] and N = 2.
On sorting the array in non-descending order we get the resulting array [1, 2].
3
4
1 -5 -5 3
5
-1 -2 3 4 5
1
-2
-5 -5 1 3
-2 -1 3 4 5
-2
Avoid creating an extra array for the merge operation. Try merging the two sorted subarrays by inserting the elements at their correct position one by one.
O(N^2*log(N)), where N is the number of elements in the array.
In the worst case, there are O(log(N)) recursive calls. For every recursive call, we merge two sorted arrays. Merge operation requires O(N) time for iterating over the subarrays, for each iteration we shift the array one step towards the right, which requires O(N) time. So in the worst case, the merge operation will take O(N^2) time. Hence, the overall time complexity is O(N^2 * log(N)) which is greater than the standard implementation.
O(log(N)), where N is the number of elements in the array.
Extra space is required for the recursion stack. Since there are O(log(N)) recursive calls. Hence, overall space complexity is O(log(N)), which is very less.