MERGE SORT

Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.

##### MERGE SORT PROCESS

Merge Sort Programming in C
 #include int i, j; /* Global function declaration */ void MergeSort(int a[],int start,int end); void Merge(int a[],int start,int middle, int end); void main() { int array[]={54, 25, 92, 18, 84, 45, 56, 20}; MergeSort(array,0,7); for(i=0; i<8; i++) { printf("\t%d",array[i]); } } /* MergeSort( ) function */ void MergeSort(int a[],int start,int end) { int mid; if(start < end) { /* Divided the current array into 2 parts */ mid = (start + end) / 2; /* Sort the first part of array */ MergeSort(a, start, mid); /* Sort the Second part of array */ MergeSort(a, mid+1, end); /* Merge the both parts by comparing elements of both the parts */ Merge(a, start, mid, end); } } /* Merge( ) function */ void Merge(int a[],int start,int middle, int end) { int b[8], k=0; i=start; j=middle+1; while(i <= middle && j <= end) { if(a[i] <= a[j]) b[k++]=a[i++]; else b[k++]=a[j++]; } /* Copy the remaining elements of first half, if there are any */ while(i <= middle) b[k++]=a[i++]; /* Copy the remaining elements of Second half, if there are any */ while(j <= end) b[k++]=a[j++]; /* Copying back the sorted list to a[ ] */ for(i=end; i>=start; i--) a[i]=b[--k]; }

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.

MORE TOPIC

WHAT WE DO