/*Program of sorting using merge sort through recursion*/
#include<stdio.h>
#define MAX 100
void merge_sort(int *arr, int left, int right)
{
int mid;
int temp[MAX];
if(left<right)/* if more than one element*/
{
mid = (left+right)/2;
merge_sort( arr, left , mid ); /* Sort arr[low:mid] */
merge_sort( arr, mid+1, right ); /* Sort arr[mid+1:right] */
/*Merge arr[low:mid] and arr[mid+1:right] to temp[left:right]*/
merge( arr, temp, left, mid, mid+1, right );
/* Copy temp[left:right] to arr[left:right] */
copy(arr,temp,left, right);
}
}/*End of merge_sort*/
/*Merges arr[left1:right1] and arr[left2:right2] to temp[left1:right2]*/
void merge( int arr[], int temp[], int left1, int right1, int left2, int right2 )
{
int i = left1;
int j = left2 ;
int k = left1 ;
while( (i <= right1) && (j <=right2))
temp[k++] = (arr[i]<=arr[j])? arr[i++]: arr[j++];
while( i <= right1 )
temp[k++]=arr[i++];
while( j <= right2 )
temp[k++]=arr[j++];
}/*End of merge()*/
void copy(int *arr, int *temp, int left, int right )
{
for(int i=left; i<=right; i++)
arr[i]=temp[i];
}
int main()
{
int i, n, arr[MAX];
printf("Enter the number of elements : ");
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i
+1); }
merge_sort( arr, 0, n-1);
printf("\nSorted list is :\n"); for(i=0; i<n; i++)
}/*End of main()*/
LypQcm9ncmFtIG9mIHNvcnRpbmcgdXNpbmcgbWVyZ2Ugc29ydCB0aHJvdWdoIHJlY3Vyc2lvbiovCgojaW5jbHVkZTxzdGRpby5oPgojZGVmaW5lIE1BWCAxMDAKCnZvaWQgbWVyZ2Vfc29ydChpbnQgKmFyciwgaW50IGxlZnQsIGludCByaWdodCkKewoJaW50IG1pZDsKCWludCB0ZW1wW01BWF07CglpZihsZWZ0PHJpZ2h0KS8qIGlmIG1vcmUgdGhhbiBvbmUgZWxlbWVudCovCgl7CgkJbWlkID0gKGxlZnQrcmlnaHQpLzI7CgkJbWVyZ2Vfc29ydCggYXJyLCBsZWZ0ICwgbWlkICk7ICAvKiBTb3J0IGFyc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