Implement Merge Sort i.e. standard implementation keeping the sorting algorithm as in-place.
In-place means it does not occupy extra memory for merge operation as in the standard case.
Approach 1: two pointers
Maintain two pointers that point to the start of the segments which have to be merged.
Compare the elements at which the pointers are present.
If element1 < element2 then element1 is at right position, simply increase pointer1.
Else shift all the elements between element1 and element2(including element1 but excluding element2) right by 1 and then place the element2 in the previous place (i.e. before shifting right) of element1. Increment all the pointers by 1.
Implementation
Below is the implementation of the above approach:
Complexity
Note: Time Complexity of above approach is O(n^2 * log(n)) because merge is O(n^2). Time complexity of standard merge sort is less, O(n log n).
Approach 2: comparing elements farly
We start comparing elements that are far from each other rather than adjacent. Basically we are using shell sorting to merge two sorted arrays with O(1) extra space.
mergeSort():
Calculate mid two split the array in two halves(left sub-array and right sub-array)
Recursively call merge sort on left sub-array and right sub-array to sort them
Call merge function to merge left sub-array and right sub-array
merge():
For every pass, we calculate the gap and compare the elements towards the right of the gap.
Initiate the gap with ceiling value of n/2 where n is the combined length of left and right sub-array.
Every pass, the gap reduces to the ceiling value of gap/2.
Take a pointer i to pass the array.
Swap the ith and (i+gap)th elements if (i+gap)th element is smaller than(or greater than when sorting in decreasing order) ith element.
Stop when (i+gap) reaches n.
Input: 10, 30, 14, 11, 16, 7, 28
Note: Assume left and right subarrays has been sorted so we are merging sorted subarrays [10, 14, 30] and [7, 11, 16, 28]
Start with
gap = ceiling of n/2 = 7/2 = 4
[This gap is for whole merged array]
10, 14, 30, 7, 11, 16, 28
10, 14, 30, 7, 11, 16, 28
10, 14, 30, 7, 11, 16, 28
10, 14, 28, 7, 11, 16, 30
gap = ceiling of 4/2 = 2
10, 14, 28, 7, 11, 16, 30
10, 14, 28, 7, 11, 16, 30
10, 7, 28, 14, 11, 16, 30
10, 7, 11, 14, 28, 16, 30
10, 7, 11, 14, 28, 16, 30
gap = ceiling of 2/2 = 1
10, 7, 11, 14, 28, 16, 30
7, 10, 11, 14, 28, 16, 30
7, 10, 11, 14, 28, 16, 30
7, 10, 11, 14, 28, 16, 30
7, 10, 11, 14, 28, 16, 30
7, 10, 11, 14, 16, 28, 30
Output: 7, 10, 11, 14, 16, 28, 30
Implementation
Complexity
Time Complexity: O(logn * nlogn)
Note: mergeSort method makes logn recursive calls and each time merge is called which takes nlogn time to merge 2 sorted sub-arrays.
Approach 3: number convertion
Suppose we have a number A and we want to convert it to a number B and there is also a constraint that we can recover number A any time without using other variable.To achieve this we choose a number N which is greater
than both numbers and add B*N in A.
so A --> A+B*N
To get number B out of (A+B*N), we divide (A+B*N) by N: (A+B*N)/N = B.
To get number A out of (A+B*N), we take modulo with N: (A+B*N)%N = A.
In a short, by taking modulo, we get old number back and taking divide we new number.
mergeSort():
Calculate mid two split the array into two halves(left sub-array and right sub-array)
Recursively call merge sort on left sub-array and right sub-array to sort them
Call merge function to merge left sub-array and right sub-array
merge():
We first find the maximum element of both sub-array and increment it one to avoid collision of 0 and maximum element during modulo operation.
The idea is to traverse both sub-arrays from starting simultaneously. One starts from l till m and another starts from m+1 till r. So, We will initialize 3 pointers say i, j, k.
i will move from l till m; j will move from m+1 till r; k will move from l till r.
Now update value a[k] by adding min(a[i],a[j])*maximum_element.
Then also update those elements which are left in both sub-arrays.
After updating all the elements divide all the elements by maximum_element so we get the updated array back.
Implementation
Complexity
Time Complexity: O(n log n) Note: Time Complexity of above approach is O(n^2) because merge is O(n). Time complexity of standard merge sort is O(n log n).