Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class Comparable>
- void merge( vector<Comparable> & a, vector<Comparable>& tmpArray,
- int leftPos, int rightPos, int rightEnd )
- {
- int leftEnd = rightPos - 1;
- int tmpPos = leftPos;
- int numElements = rightEnd - leftPos + 1;
- while( leftPos <= leftEnd && rightPos <= rightEnd )
- if( a[ leftPos ] <= a[ rightPos ] )
- tmpArray[ tmpPos++ ] = a[ leftPos++ ];
- else
- tmpArray[ tmpPos++ ] = a[ rightPos++ ];
- while( leftPos <= leftEnd ) // Copy rest of first half
- tmpArray[ tmpPos++ ] = a[ leftPos++ ];
- while( rightPos <= rightEnd ) // Copy rest of right half
- tmpArray[ tmpPos++ ] = a[ rightPos++ ];
- // Copy tmpArray back
- for( int i = 0; i < numElements; i++, rightEnd-- )
- a[ rightEnd ] = tmpArray[ rightEnd ];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement