views:

1598

answers:

1

I am trying to implement Median of Medians in Java for a method like this:

Select(Comparable[] list, int pos, int colSize, int colMed)
  • list is a list of values of which to find a specified position
  • pos is the specified position
  • colSize is the size of the columns that I create in the first stage
  • colMed is the position in those columns that I use as the medX

I am not sure which sorting algorithm would be the best to use or how to implement this exactly..

A: 

I don't know if you still need this problem solved, but http://www.ics.uci.edu/~eppstein/161/960130.html has an algorithm:

select(L,k)
{
    if (L has 10 or fewer elements)
    {
        sort L
        return the element in the kth position
    }

    partition L into subsets S[i] of five elements each
        (there will be n/5 subsets total).

    for (i = 1 to n/5) do
        x[i] = select(S[i],3)

    M = select({x[i]}, n/10)

    partition L into L1<M, L2=M, L3>M
    if (k <= length(L1))
        return select(L1,k)
    else if (k > length(L1)+length(L2))
        return select(L3,k-length(L1)-length(L2))
    else return M
}

Good luck!

Chip Uni
I know this is an old post, but the above implementation is not really the median of medians, since it contains a call to sort, it's worst case will be minimum O(nlgn). you need to find the median of each group of five using a constant number of comparisons (the problem i am looking to solve, I think I might have it, it uses 4 comparisons in every case. , I can't write it here, since it is on 44 lines! If you et rid of the call to sort, this algorithm will be O(n)
Tom