I have to write an efficient pseudocode to find the i-th smallest element in an unsorted array with n elements,where n ,i and A[n] are input data.
In my opinion this means that I have to write the pseudocode for sorting an array in increasing order.I was about to do it with selection sort but I read the question again and it said efficient so lets do it with merge sort!
My pseudocode is :
{
n<---length[A]
if (n<2)
return
mid <--- n/2
left<---array of size(mid)
right<---array of size(n- mid)
for i<---0 to mid-1
left[i]<--- A[i]
for i<---mid to n-1
right [i-mid]<---A[i]
Mergesort(left)
Mergesort(right)
Merge(left,right,A)
}
The complexity in all the cases (worst average best) is O(nlogn).Which means that this algorithm is very fast !
Is my solution correct
Edit: My question is very specific,it asks for an efficient algorithm.Which means we need a fast one.The ones you keep trying to add as duplicate arent efficient they have O(n) complexity