Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. template <class Comparable>
  2. void merge( vector<Comparable> & a, vector<Comparable>& tmpArray,
  3. int leftPos, int rightPos, int rightEnd )
  4. {
  5. int leftEnd = rightPos - 1;
  6. int tmpPos = leftPos;
  7. int numElements = rightEnd - leftPos + 1;
  8. while( leftPos <= leftEnd && rightPos <= rightEnd )
  9. if( a[ leftPos ] <= a[ rightPos ] )
  10. tmpArray[ tmpPos++ ] = a[ leftPos++ ];
  11. else
  12. tmpArray[ tmpPos++ ] = a[ rightPos++ ];
  13. while( leftPos <= leftEnd ) // Copy rest of first half
  14. tmpArray[ tmpPos++ ] = a[ leftPos++ ];
  15. while( rightPos <= rightEnd ) // Copy rest of right half
  16. tmpArray[ tmpPos++ ] = a[ rightPos++ ];
  17. // Copy tmpArray back
  18. for( int i = 0; i < numElements; i++, rightEnd-- )
  19. a[ rightEnd ] = tmpArray[ rightEnd ];
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement