|
C
For Swimmers : Mastering C step-by-step |
|
|
|
Merge
Sort
|
|
Definition :
Merge-sort is based on the divide-and-conquer paradigm. The Merge-sort algorithm can be described in general terms as consisting of the following three steps:
1. Divide Step
If given array A has zero or one element, return S; it is already sorted. Otherwise, divide A into two arrays, A1 and A2, each containing about half of the elements of A.
2. Recursion Step
Recursively sort array A1 and A2.
3. Conquer Step
Combine the elements back in A by merging the sorted arrays A1 and A2 into a sorted sequence.
|
ALGORITHM : MERGE SORT
To begin, suppose that we have two sorted arrays A1[1],
A1[2], . . , A1[M]
and A2[1], A2[2], . .
. , A2[N]. The following is a
direct algorithm of the obvious strategy of successively
choosing the smallest remaining elements from A1
to A2 and putting it in A.
|
MERGE
(A1, A2, A)
i =
j1
A1[m+1], A2[n+1] =
INT_MAX
For k = 1 to m + n
do
if A1[i] < A2[j]
then A[k] =
A1[i]
i = i +1
else
A[k] = A2[j]
j = j + 1
The Merge sort algorithm is as
follows:
MERGE_SORT
(A)
A1[1 . .
floor(n/2)] = A[1
. . floor(n/2)]
A2[1 . . floor(n/2)] =
A[1 + floor(n/2) . . n]
Merge Sort (A1)
Merge Sort (A1)
Merge Sort (A1, A2,
A)
|
|
FREE
Source Code
Click
Here: Merge Sort - Ascending Order
|
|
Animation Effect
|
Click
Here : To learn more about Merge Sort
|
|
|
|
You are
Visitor No. 
Sign
my Guestbook
View
my Guestbook
Thanks for using C For Swimmers.
Regarding this material, you can send Bug Reports,
Suggestions, Comments, etc. to
nandakishorkn@rediffmail.com
|