/* ECP: FILEname=fig11_23.c */ /* 1*/ #define SwapElements( i, j ) \ /* 2*/ { ElementType Tmp = A[ i ]; A[ i ] = A[ j ]; A[ j ] = Tmp; } /* 3*/ static int /* 4*/ Partition( ElementType A[ ], int Low, int High ) /* 5*/ { /* 6*/ int Center = ( Low + High ) / 2; /* 7*/ const ElementType Pivot = A[ Center ]; /* 8*/ int i = Low - 1, j = High; /* 9*/ SwapElements( Center, High ); /*10*/ for( ; ; ) /*11*/ { /*12*/ while( A[ ++i ] < Pivot ) /* Find A "large" Element */ /*13*/ ; /* i < High Is Guaranteed */ /*14*/ while( A[ --j ] > Pivot ) /* Find A "small" Element */ /*15*/ if( j == 0 ) /* Don't run off the end! */ /*16*/ break; /*17*/ if( i >= j ) /*18*/ break; /*19*/ /* Invariant: i < j, A[ i ] >= Pivot, A[ j ] <= Pivot */ /*20*/ SwapElements( i, j ); /*21*/ } /*22*/ SwapElements( i, High ); /*23*/ return i; /*24*/ }