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;
}