1

So i got into a bit of an argument with my CS professor today about whether or not the partitioning function that is part of my quicksort algorithm qualifes as a Hoare partition. According to her, there is only one specific way to implement a Hoare partition, and everything else is wrong.

This led me to wonder, is there really a specific definition of a Hoare partition? I always assumed that a Hoare partition was any partitioning algorithm that sorted using the method that Tony Hoare developed (i.e. two iterators traversing a list in opposite directions); i did not think that the exact implementation of the algorithm actually mattered. Is this correct, or is there really only one "correct" way to implement Hoare's algorithm.

Here is my partitioning algorithm:

template <typename T>
int partArray(T* array, int leftIndex, int rightIndex)
{
    int pivot = leftIndex, middle = (leftIndex + rightIndex) / 2, leftIterator = leftIndex + 1, rightIterator = rightIndex;

    //Move median element into pivot position
    if (array[leftIndex] > array[rightIndex])
        SWAP(array[leftIndex], array[rightIndex]);
    if (array[middle] > array[rightIndex])
        SWAP(array[middle], array[rightIndex]);
    if (array[middle] > array[leftIndex])
        SWAP(array[middle], array[leftIndex]);

    //Partition array using Hoare's algorithm
    while (true)
    {
        while (array[leftIterator] <= array[pivot] && leftIterator < rightIndex) leftIterator++;
        while (array[rightIterator] >= array[pivot] && rightIterator > leftIndex) rightIterator--;
        if (leftIterator < rightIterator)
            SWAP(array[leftIterator], array[rightIterator]);
        else
            break;
    }
    SWAP(array[pivot], array[rightIterator]);

    //Return final position of pivot
    return rightIterator;
}
android927
  • 203
  • 1
  • 12
  • Why not use `std::swap` which is specialized for an efficient swap for certain classes. – Neil Kirk Nov 10 '14 at 17:06
  • How does anything change for anybody, including you, if the answer is "yes" vs "no"? – Nemo Nov 10 '14 at 17:07
  • If memory serves, the partitioning method you specify (iterators traversing the list in opposite directions) was invented by Robert Sedgewick, not Tony Hoare. As far as I know, Hoare originally only described the partitioning in terms of the effect it was to produce. – Jerry Coffin Nov 10 '14 at 17:34
  • @JerryCoffin I'm just going by what my CS prof has told me. – android927 Nov 10 '14 at 17:46
  • @MikeSeymour It does partition correctly, and i don't see how my nested loops are any more convoluted than any other implementation of this algorithm. Can you provide an example as to how exactly they are convoluted? – android927 Nov 10 '14 at 17:50
  • @JerryCoffin I do believe, however, that [most people](http://stackoverflow.com/questions/12544908/explanation-of-hoare-partitioning-algorithm) consider this style of partition to be a "Hoare partition" – android927 Nov 10 '14 at 17:53
  • @MikeSeymour The double nested loop is what makes it a Hoare partition. See [here](http://stackoverflow.com/questions/12544908/explanation-of-hoare-partitioning-algorithm) and [here](http://stackoverflow.com/questions/7198121/quicksort-and-hoare-partition). Even the example in my text book has a similar double-nested-loop structure. – android927 Nov 10 '14 at 17:59
  • @android927: Whatever. This isn't the place for a code review, especially if you just came here for an argument. – Mike Seymour Nov 10 '14 at 18:03
  • @MikeSeymour I did not come here for either a code review or an argument. I came to ask the following question: Does a partitioning algorithm need to fit a strict definition in order to be considered a Hoare partition? – android927 Nov 10 '14 at 18:09
  • @NeilKirk Does std::swap work for templated datatypes? – android927 Nov 12 '14 at 16:18
  • 1
    @android927 It works with anything that can be copied, or moved (C++11) or provides a custom swap. It is itself a templated function. – Neil Kirk Nov 12 '14 at 16:23
  • @Nemo I suppose that nothing would change. However, where would we be as a society if we always refused to seek out new information that does not directly change anything for us? Must every question we ask be justified by some underlying purpose? Should we no longer ask questions in our quest for knowledge if the knowledge gained does not directly benefit us in some way? – android927 Nov 12 '14 at 16:32

0 Answers0