Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. //LeftPos = start of left half;
  2. //RightPos = start of right half
  3. void Merge(int A[ ], int LeftPos, int RightPos, int RightEnd)
  4. {
  5. int LeftEnd = RightPos – 1;
  6. int TmpPos = 1
  7. int NumElements = RightEnd – LeftPos + 1;
  8. int TempArray[NumElements];
  9. while(leftPos <= LeftEnd && RightPos <= RightEnd)
  10. if(A[LeftPos] <= A[RightPos])
  11. TmpArray[TmpPos++] = A[LeftPos++];
  12. else
  13. TmpArray[TmpPos++] = A[RightPos++];
  14. while(LeftPos <= LeftEnd) //Copy rest of first half
  15. TmpArray[TmpPos++] = A[LeftPos++];
  16. while(RightPos <= RightEnd) //Copy rest of second half TmpArray[TmpPos++] = A[RightPos++];
  17. for(int i = 1; i <= NumElements; i++) //Copy TmpArray back
  18. A[LeftPos++] = TmpArray[i];
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement