Algorithm:Sort using Median and Just 1 comparison in $ O(n \log n) $
I am trying to write an sorting algorithm which takes an input array and
produces the sorted array. Following are the constraints for an algorithm.
Time complexity = $O(n \log n)$
Only one comparison operation throughout the algorithm.
We have a function which can provide an median for a set of three elements.
I have tried finding a solution and following is my result.
we use the median function to get the smallest and the largest pair.
Example: Give an array A[1..n], we get the median of the first three
elements, lets call it a set and we receive a $Median$. In next Iteration
we remove the received median from the set and add next element to the set
and again call Median function. This step when repeated over the length
produces a pair of the largest and the smallest element for the entire
array.
We use this pair, use the comparison operation and place them at position
A[0] & A[n-1].
We repeat the same operation over the array A[1..n-2] to get another pair
of the largest and smallest.
We take the median with the A[0] and newly received pair.
Median Value is placed at the A[1].
We take the median with the A[n-1] and newly received pair.
Median Value is placed at the A[n-2].
Step 3~7 are repeated to get a sorted array.
This algorithm satisfies the condition 2 & 3 but not the time complexity.
I hope if someone can provide some guidance how to proceed further.
No comments:
Post a Comment