So I have been working on properly implementing a quicksort algorithm for a homework assignment all night and after searching for a couple of hours I couldn’t find any concrete implementation of the median partitioning. So, I am stuck at a huge roadblock where I don’t know what part of my algorithm is horribly broken, and since all of the algorithms online are pseudo-code (so their description of the partitioning is excruciatingly vague), it’s impossible for me to check against my implementation. So here is my Frankenstein code after reading document after document of how a median of medians pivot selection would work with quicksort in java.
Here are the only sources I’ve found even remotely helpful so far, and they are all painfully vague on the parts that make the algorithm work, deliberately it would seem after spending so much time searching:
http://mitpress.mit.edu/algorithms/solutions/chap9-solutions.pdf
http://www.cs.umd.edu/~meesh/351/mount/lectures/lect9-medians-selection.pdf
*http://www.ics.uci.edu/~eppstein/161/960130.html
*http://en.wikipedia.org/wiki/Selection_algorithm
Here is my quicksort algorithm:
{Removed Code}
And here is my selection method, I think the horribly messed up part is how I select the medians, and median of medians, but I couldn’t find anything online to guide me on the partitioning:
{Removed Code}
Also for my partitioning I used my regular quicksort partition method but changed it slightly so the pivot is given as a parameter, I don’t know if there was anything else I was supposed to do since all of the sources online are vague in this area as well:
{Removed Code}
Here is my insertion sort for sorting the groups of 5, just in case:
{Removed Code}
Any insight into what part of the algorithm I screwed up would be immensely appreciated as I’ve been banging my head against a wall trying to find whats wrong. There is definitely a giant mistake in the selection algorithm since my JUnit test fails against a presorted array of [1..100] for selecting the median where the expected result is 50, but my algorithm always spits out 54.
Where you should pick the median from the 5-element “subsets” you are picking the element next to it: the median would have the index
2but you are picking the index3. You need:By the way, the “median of medians” of [1..100] would be 48 or 53, not 50. Write the numbers in a grid of 5 by 20: the values in the middle row (3,8,13,…) are the medians of the “subsets”.