You are given an array ‘ARR’ consisting of 'N' integers, and your task is to sort the given array in non-decreasing order using the Heap sort algorithm.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains a single integer 'N', as described in the problem statement.
The second line of each test case contains 'N' space-separated integers, representing the elements of the array.
Output Format:
For each test case print 'N' space-separated integers arranged in non-decreasing order.
The output of every test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
-10^9 <= ARR[i] <= 10^9
Where ‘T’ denotes the number of test cases and ‘N’ denotes the size of ‘ARR’.
Time Limit: 1sec
1
4
10 7 8 11
7 8 10 11
For the first test case, the array [10,7,8,11] will be converted into [7,8,10,11] after arranging into non-decreasing order.
1
5
5 -2 3 -1 8
-2 -1 3 5 8
For the first test case, the array [5,-2,3,-1,8] will be converted into [-2,-1,3,5,8] after arranging into non-decreasing order.
Try to implement the entire heap sort algorithm.
Approach:
Example: let the given array is [5 2 1 3 12 8].
O(N * log(N)), where N is the size of ‘ARR’.
Since the Heapification take order of O(log(N)) time and we are applying Heapification N times, therefore the time complexity is of the order of O(N*log(N)).
O(1).
Since we are using constant extra space the space complexity is O(1).