Given an array which is having elements which are in increasing order till a max value and then numbers in decreasing order.
Eg. int a[] = { 10, 12, 14, 16, 15, 13, 11}.
How can this array be sorted efficiently?
The array needs to be sorted in-place.
My solution:
Take 2 pointers start of array and end of array.
Into result array write a min(or max if you need sort in descending) value from both pointers, and shift
pointer to 1 position (start pointer +1 position and end pointer -1
position
Repeat till start pointer will be placed after end pointer.
Complexity of solution is O(N).
Required memory is O(N)
Pseudocode:
Solution for updated question:
As solution usde partil sorting of first part of array.
Pointers on (10 and 11)
{10, 12, 14, 16, 15, 13, 11}
Pointers on (12 and 11)
Swap 12 and 11
{10, 11, 14, 16, 15, 13, 12}
Pointers on (14 and 12)
Swap 14 and 12
{10, 11, 12, 16, 15, 13, 14} // Move pointer from 14 to 13 a it lesser.
Now we have sorted {10, 11, 12} and sub problem for {16, 15, 13, 14} (N for sub problem decreased twicely)
Complexity for this algorithm is: O(N) + (N/2) + O(N/4) + … totally is O(N)
Image for better illustration: