Sunday 15 May 2011

c++ - Strict definition of Hoare partition? -



c++ - Strict definition of Hoare partition? -

so got bit of argument cs professor today whether or not partitioning function part of quicksort algorithm qualifes hoare partition. according her, there 1 specific way implement hoare partition, , else wrong.

this led me wonder, there specific definition of hoare partition? assumed hoare partition partitioning algorithm sorted using method tony hoare developed (i.e. 2 iterators traversing list in opposite directions); did not think exact implementation of algorithm mattered. correct, or there 1 "correct" way implement hoare's algorithm.

here 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 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 homecoming rightiterator; }

c++ algorithm quicksort partitioning

No comments:

Post a Comment