The merge sort is a sorting algorithm that uses the divide and conquer strategy . in this method ,division is dynamically carried out .
Merge sort on an input array with n elements consists of three steps.
1) Divide : Partition array into two sublists s1 and s2 with n/2 elements each .
2) Conquer : Recursively sort s1 and s2 .
3) Combine : Merge s1 and s2 into a unique sorted group .
The merge sort divides the given list into two sublists recursively until each sublist contains only one element .Then we apply the same merging process recursively as explained above to obtain the sorted list . If the single file or list contains N elements then it is divided into two sublists containing N/2 elements which are further divided into sublists containing N/4 elements and so on.
Also check : Program for Insertion sort
Here a is a linear array , variables lb ,lr represents beginning and ending indices of the left sub array and variables rb ,rr represents beginning and ending indices of the right sub array .The algorithm merges these two sub arrays .It uses local variables na ,nb as counters to keep track of the elements of left and right sub array respectively which are candidates to go to temporary array c and nc as counter to keep track of the position in c , where the incoming elements will be stored .
After the procedure for merging to halves is defined the various steps required for mergesort are summarized in the following algorithm ,
Merge sort on an input array with n elements consists of three steps.
1) Divide : Partition array into two sublists s1 and s2 with n/2 elements each .
2) Conquer : Recursively sort s1 and s2 .
3) Combine : Merge s1 and s2 into a unique sorted group .
The merge sort divides the given list into two sublists recursively until each sublist contains only one element .Then we apply the same merging process recursively as explained above to obtain the sorted list . If the single file or list contains N elements then it is divided into two sublists containing N/2 elements which are further divided into sublists containing N/4 elements and so on.
Also check : Program for Insertion sort
Algorithm for Merge sort :
Mergingsortedsubarrays(a , lb , lr , rb , rr)Here a is a linear array , variables lb ,lr represents beginning and ending indices of the left sub array and variables rb ,rr represents beginning and ending indices of the right sub array .The algorithm merges these two sub arrays .It uses local variables na ,nb as counters to keep track of the elements of left and right sub array respectively which are candidates to go to temporary array c and nc as counter to keep track of the position in c , where the incoming elements will be stored .
Begin
set na=lb, nb=rb and nc=lb
while((na<=lr) and (nb<=rr)) do
if(a[na]<b[nb]) then
Set c[nc]=a[na]
set na=na+1
else
set c[nc]=a[nb];
set nb=nb+1;
endif
setnc=nc+1
endwhile
if(na>lr) then
while(nb<=rr)then
set c[nc]=b[nb];
set nb=nb+1;
set nc=nc+1;
endwhile
else
while(na<=lr) then
set c[nc]=b[na];
set na=na+1;
set nc=nc+1;
endwhile
endif
for k=lb to rr by 1 do
set a[k]=c[k]
endfor
End
After the procedure for merging to halves is defined the various steps required for mergesort are summarized in the following algorithm ,
Algorithm : Mergesort(a , beg , end)
Here a is a linear array with lower bound beg and upper bound end .The algorithm sorts the array using merge sort method .It uses variable mid to represent the index of the element at which array will be divided into two halves .Begin
if(beg<end) then
set mid = (beg+end)/2;
call mergesort(a,beg,mid);
call mergesort(a,mid+1,end);
call mergingsortedsubarray(a,beg,mid,mid+1,end)
endif
End