|Author: Noorul Hasan 09 Jul 2012 Member Level: Silver Points : 10 (Rs 6) Voting Score: 0|
The working of merge sort is as follows:
1) First you divide the number of elements by 2 and seperate them as two.
2) Divide those two which are divided by 2.
3) Divide them until you get a single element.
4) Start comparing the starting two pair of elements with each other and place them in ascending order.
5) When you combine them compare them so that you make sure they are sorted.
6) When all the elements are compared the array will be surely sorted in an ascending order.
Algorithm for merge sort is:
// For 5 elements
Step 2:Declare and low,mid,high where low=a, mid= (low + high)/2 and high = a
Step 3:check for the condition if (low < high)
Step 4:Merge (item, low, mid)
Step 5:Merge (item, mid+1, high)
Step 6:For (I=low, J=mid +1, K=low; I<=mid AND J<=high; K++)
Step 7:If (item [I]
Step 8:Set temp [K] is equals to item [I] i++
Step 10: set temp [K] is equals to item [J] j++
[ end if ]
[ end for loop ]
Step 11:While (I<=mid)
Step 12:Set temp [K] = item [I] i++ k++
Step 13:While (J<=high)
Step 14:Set temp [K] = item [J] j++ k++
Step 15:For (K=low; K<=high; K++) Update item [K] as temp [K];
[End for ]
[End while loop 2]
[ End while loop 1]
Step 16: Stop
|Author: Adi 09 Jul 2012 Member Level: Silver Points : 10 (Rs 5) Voting Score: 0|
I am giving you the code in C.Refer it.Use "code blocks" compiler,if it does not run in turbo c.
For running it in turbo c make some changes in the code,like add the headers and clrscr(),getch(),etc.
See the attachment.