Introduction
Merge sort:
The merge sort technique sorts a given set of values by combining two sorted arrays into one larger sorted array. Consider a sorted array, A which contains p elements, and the sorted array B, containing q elements.
The merge sort technique combines the elements of A and B into single sorted array C with p+q elements. The first data item of the A array is compared with the first data item of the B array. If the first data item of A is smaller than the first data item of B, then that data item from A is moved to the array, C. If the data item of B is smaller than the data item of A, then it is moved to array, C. This comparing of data items continues until one of the array ends.
#include <stdio.h> void merge(int *left,int *right,int *result,int nl,int nr) { int i=0,j=0,k=0; while(nl>0 && nr>0) { if(left[i] <= right[j]) { result[k] = left[i]; i++; k++; nl--; } else { result[k] = right[j]; j++; k++; nr--; } } while(nl>0) { result [k] = left[i]; i++; k++; nl--; } while(nr>0) { result[k] = right[j]; j++; k++; nr--; } } void main() { int a[] = {11,33,95}; int b[] = {45,82,94}; int c[6],i; printf("\n The first array is: {11,33,95}"); printf("\n The second array is: {45,82,94}"); merge(a,b,c,3,3); printf("\n The sorted list is: "); for(i=0;i<6;i++) printf("\n %d",c[i]); }
dharmendra 6/29/2011 (IST) / Reply
adasd
anil 10/3/2011 (IST) / Reply
its really nice code renuka garu...... very clear, can understand easily in first look......
Kuldeep Rishi 11/17/2012 (IST) / Reply
A good programme must have descriptive comment. this piece of code lacks comments. would have been much more useful with comments
Anjali 1/2/2013 (IST) / Reply
nice code...thanx...
prakash 3/18/2013 (IST) / Reply
thanx.., ..really nice, easy, useful and perfect explaination....& coding. :)
dathu 4/25/2013 (IST) / Reply
Excellant very nice
Rajpal Diwaker 5/7/2013 (IST) / Reply
why use data structure in c language
satyesh 6/15/2013 (IST) / Reply
this program is not working more and same value such as a[] have 10,2,5,7,30 and b[] have 2,34,11,5 it's not working.....try it...and give me reply plz...sir.....