finding the middle value without sorting [duplicate]
Possible Duplicate:
How to find the kth largest element in an unsorted array of length n in O(n)?
Is there any algorithm of finding the middle value of given list without performing the sorting operation.
for example if the list is maintained as stack and contains ( 1,2,5,3,4,6 ) then the middle value should be 4 so my question is how to get this value without performing t开发者_C百科he sorting operation on the list.
is this possible?
Yes, there are well-known algorithms to find the kth largest element of a list. See How to find the kth largest element in an unsorted array of length n in O(n)?
Here k
is pre-computed as length/2
of course, adjusted to the nearest integer if there are an even number of elements in your list.
精彩评论