/////////////////////////////// QuickSort /////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////// void QuickSort( zULONG ulRecord1, // recursive zULONG ulRecord2 ) { static WORD wRecurseCnt = 0; wRecurseCnt++; #ifdef DEBUG_ALL static WORD wMaxRecurse = 0; if ( wMaxRecurse < wRecurseCnt ) { wMaxRecurse = wRecurseCnt; } #endif if ( ulRecord1 < ulRecord2 ) { zULONG ul = QuickSortPartition( ulRecord1, ulRecord2 ); QuickSort( ulRecord1, ul ); QuickSort( ul + 1, ulRecord2 ); } wRecurseCnt--; #ifdef DEBUG_ALL if ( wRecurseCnt == 0 ) { ASSERT_TRACE( "Maximum quicksort recurse count = %d", wMaxRecurse, FALSE ); // OutputDebugString( "Maximum recurse count = " ); // ltoa( wMaxRecurse, szBuffer, 10 ); // OutputDebugString( szBuffer ); // OutputDebugString( "\n" ); } #endif }
void QuickSort(void** Table, int Size, CompCallback Callback) { void* Pivot = NULL; int PivotIdx = 0; //A table consisting of one element is always sorted. if(Size <= 1) return; /** * Find the pivot, then put all elements that are less than the pivot on the left of its * and all elements that are greater on the right of the pivot, then subdivde the table * in to two and repeat. */ Pivot = MedianPivot(Table, Size); PivotIdx = QuickSortPartition(Table, Size, Pivot, Callback); QuickSort(Table, Size - (Size - PivotIdx), Callback); QuickSort(Table + PivotIdx, (Size - PivotIdx), Callback); }
/////////////////////////////// QuickSortNR ///////////////////////////////// // ///////////////////////////////////////////////////////////////////////////// void QuickSortNR( zULONG ulRecord1, // non-recursive zULONG ulRecord2 ) { ASSERT_TRACE( "QuickSort argument error", 0, ulRecord1 <= ulRecord2 ); zULONG m = ulRecord2 - ulRecord1; zULONG ulStackSize = 2; while ( m >>= 1 ) { ulStackSize++; } ulStackSize <<= 2; // two entries for each comparison zPULONG pulStack = (zPULONG) m_pPoolVarData->Alloc( ulStackSize * sizeof( zULONG ) ); zULONG ulStack = 0; zULONG uLeft; zULONG uRight; #ifdef DEBUG_ALL zULONG ulStackMax = 0; #endif pulStack[ ulStack++ ] = ulRecord1; pulStack[ ulStack++ ] = ulRecord2; while ( ulStack ) { uRight = pulStack[ --ulStack ]; uLeft = pulStack[ --ulStack ]; m = QuickSortPartition( uLeft, uRight ); #ifdef DEBUG_ALL ShowArray( uLeft, uRight ); #endif if ( m + 1 < uRight ) { #ifdef DEBUG_ALL if ( ulStack > ulStackSize - 2 ) { ASSERT_TRACE( "QuickSort stack overflow - %d", ulStackSize, FALSE ); m_pPoolVarData->Free( pulStack ); return; } #endif pulStack[ ulStack++ ] = m + 1; pulStack[ ulStack++ ] = uRight; #ifdef DEBUG_ALL if ( ulStack > ulStackMax ) { ulStackMax = ulStack; } #endif } if ( uLeft < m ) { #ifdef DEBUG_ALL if ( ulStack > ulStackSize - 2 ) { ASSERT_TRACE( "QuickSort stack overflow - %d", ulStackSize, FALSE ); m_pPoolVarData->Free( pulStack ); return; } #endif pulStack[ ulStack++ ] = uLeft; pulStack[ ulStack++ ] = m; #ifdef DEBUG_ALL if ( ulStack > ulStackMax ) { ulStackMax = ulStack; } #endif } } m_pPoolVarData->Free( pulStack ); #ifdef DEBUG_ALL ASSERT_TRACE( "Max stack utilization = %d\tAllocated stacksize = %d", (ulStackMax, ulStackSize), FALSE ); #endif }