void Quicksort( ItemType* base, size_t nelem, int (*fcmp)( const ItemType&, const ItemType& ) ) { if( nelem<=1 ) return; size_t pivot = Partition( base, nelem, fcmp ); unsigned LHS = pivot; unsigned RHS = nelem - (LHS+1); if( LHS!=0 ) Quicksort( base, LHS, fcmp ); if( RHS!=0 ) Quicksort( (base+pivot+1), RHS, fcmp ); } // Partition has a pivot and a oe/other end. If one element is out of order // then pivot and oe switch. This means pivot is on the right and oe is on the // left. Starting off, pivot_on_left=true but every time it has to switch with oe // pivot_on_left becomes false. After a swap, oe moves one element closer to pivot. size_t Partition( ItemType* base, size_t nelem, int (*fcmp)( const ItemType&, const ItemType& ) ) { int pivot = 0; bool pivot_on_left=true; int oe = (int)(nelem-1); while( pivot!=oe ) { // If (pivotleft and out of order) OR (if pivotright and ooo) if( (pivot_on_left && (fcmp(base[pivot], base[oe])>0)) || (!pivot_on_left && (fcmp(base[pivot], base[oe])<0)) ) { //swap( *pivot, *oe ); ItemType temp = base[pivot]; base[pivot] = base[oe]; base[oe] = temp; //swap( pivot, oe ); int ptemp = pivot; pivot = oe; oe = ptemp; pivot_on_left = !pivot_on_left; } // oe moves towards pivot if( pivot_on_left ) oe--; if( !pivot_on_left ) oe++; } }

Now in a separate project I am trying to generalize my Quicksort function with the use of void pointers. I have also defined a few other functions to help assist Quicksort with void pointers. They look as follows.

// Swaps two entire array elements using size w // w will be passed in through main as sizeof(ItemType) void swap( void* a, void* b, size_t w) { char* ch1 = reinterpret_cast<char*>(a); char* ch2 = reinterpret_cast<char*>(B)/>; for(unsigned i=0; i<w; i++) { char temp = ch1[i]; ch1[i]=ch2[i]; ch2[i]=temp; } } // Assists in moving oe one unit closer to pivot. If the bool // is true, then pivot is on left and oe moves left towards pivot. // The opposite for pivot on right. void move( void*& a, size_t w, bool b ) { reinterpret_cast<char*&>(a) += (B)/>? w:-w; }

Now my real problem is coming back to the Quicksort and Partition function for void pointers, I can't seem to figure out how to apply my swap() and move() functions. This is the code I have for Quicksort() which takes a void pointer. It seems clear to me I am doing something wrong because I really don't understand what I'm trying to do at this point. Thanks again if you read all the way through!

void Quicksort( void* base, size_t nelem, size_t width, int (*fcmp)( const void*, const void* ) ) { if( nelem<=1 ) return; size_t pivot = Partition( base, nelem, width, fcmp ); unsigned LHS = pivot; unsigned RHS = nelem - (LHS+1); if( LHS!=0 ) Quicksort(base, LHS, width, fcmp ); if( RHS!=0 ) Quicksort( ((char*)base+pivot+1), RHS, width, fcmp ); }