GeeksforGeeks » Interview Questions
Sort an array which is already sorted in two parts
(8 posts)-
Given an array with two subparts sorted. How will you make a final sorted array.
i/p:
1, 5, 7, 9, 11, 23, 2, 3, 8, 9, 21o/p:
1, 2, 3, 5, 7, 8, 9, 9, 11, 21, 23 -
use the merge function we use in merge-sort to merge two sorted arrays. It will be O(n) complexity
-
For finding the index of two arrays...first traverse until the point where the array changes its order....example here after 23 its 2.....thats the index of the second array...pass the two index to merge function.....
-
merging requires O(n) extra space we need to do it O(1) space & O(n) Time
-
to do it in O(1) space we will need to move all elements of 1st subarray to right when we have to insert element of 2nd subarray. This will reduce space complexity but will increase time complexity.
example
1,5,7,9,11,23,2,3,8,9,21
1st subarray -- 1, 5, 7, 9 ,11 , 23
2nd subarray - 2,3,8,9,21
To sort this arraywhen we need to insert 2 after 1 all elements of 5,7,9,11,23 will be needed to move rightwards by one position resulting in
1, 2,5,7,9,11,23,3,8, 9,21
so this increases time complexity.
Is there any efficient method
-
The complexity with merger sort will be of Order O(nlogn) .
We can also do this by using binary search and some swapping
For instance for I/P : 1, 5, 7, 9, 11, 23, 2, 3, 8, 9, 21
By applying Binary search we can find the element Ei such that : E (i-1) < Ei > E (i+1) .
Runtime Complexity O(log n)
Then swap the elements from two parts of the matrix from index :
a=1 to i
&
b= (i+1) to n .Then swap the elements for which Ea < Eb (This again can be done by Binary Search ) , then place the value of Ea in the other array where it has been swapped in ascending order .
RunTime Complexity O(n)Time Complexity is reduced to some extend with this approach .
Illustration ::
1, 5, 7, 9, 11, 23, 2, 3, 8, 9, 21
Apply binary Search on this for the element Ei such that : E (i-1) < Ei > E (i+1) .
start index = 1 (assume)
So we got the index = 6
Now take to variable
a=1 to 6
b=7 to 121, 5, 7, 9, 11, 23, 2, 3, 8, 9, 21
Step 1 : For Ea there is no element which is > Ea (Binary Search again For the Element 1 in the subarray from b=7 to 12 ) , a++ .
Step 2 :For Ea there is element which is just > Ea (Binary Search again For the Element 1 in the subarray from b=7 to 12 ) , so swap 5 with 2 .
1, 2, 7, 9, 11, 23, 5, 3, 8, 9, 21 , a++;
Now adjust 5 in ascending order. so the final array becomes : 1, 2, 7, 9, 11, 23, 3, 5, 8, 9, 21Step 3: Again the same process : Swap 7 and 3 and adjust 7 in the other subarray with index b.
continue this step . And finally you will get the sorted array .
RunTime Complexity : O(log n + Ilog(n-I)) I is the index Where we Partition the array . (Ei)
In Worst Case RunTime Complexity O(logn + n) ~ O(n) . -
@ darkprince
I think for a case like "1 2 3 77 89 123 4 5 6 7 8"
order will be O(n^2) if we consider shifting of elements
So the worst case order is not O(n) . -
@ darkprince I think for a case like "1 2 3 77 89 123 4 5 6 7 8" order will be O(n^2) if we consider shifting of elements So the worst case order is not O(n) .
Reply
You must log in to post.