In the card-playing motif,suppose we have two piles of cards face up on a table.Each pile is sorted,with the smallest cards on top.We wish to merge the two piles into a single sorted output pile,which is to be face down on the table.Our basic step consists of choosing the smaller of the two cards on top of of the face_up piles,removing it from its pile(which expsoe a new top card),and placing this card face down onto the output pile.We repeat this step until one input pile is empty,at which time we just take the remaining input pile and place it face down onto the output pile.
Let’s show it.








































So,we get the merge function.Now sort the array is easy.We use the Merge peocedure as a subroutine in the merge sort algrithm.
public static void MergeSort(Int32[] arr, Int32 p, Int32 r)
{
if (p < r)
{
Int32 q = (p + r) / 2;
MergeSort(arr, p, q);
MergeSort(arr, q + 1, r);
Merge(arr, p, q, r);
}
}
If p>=r,the subarray has at most one element and is therefore already sorted.
The Merge sort’s running time is big theta n*lgn.