#include<stdio.h>
void mergesort(int arr[], int i, int j);
void merge(int arr[], int i1, int j1, int i2, int j2);
int main()
{
int arr[100], n, i, j;
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\n= = = = = = = = = = = Merge Sort in C = = = = = = = = = = = =");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
mergesort(arr,0,n-1);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void mergesort(int arr[],int p,int q)
{
int mid;
if(p<q)
{
mid=(p+q)/2;
mergesort(arr,p,mid);
mergesort(arr,mid+1,q);
merge(arr,p,mid,mid+1,q);
}
}
void merge(int arr[], int i1, int j1, int i2, int j2)
{
int temp[100];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(arr[i]<arr[j])
{
temp[k]=arr[i];
i++;
}
else
{
temp[k]=arr[j];
j++;
}
k++;
}
while(i<=j1)
{
temp[k]=arr[i];
k++;
i++;
}
while(j<=j2)
{
temp[k]=arr[j];
k++;
j++;
}
for(i=i1,j=0; i<=j2; i++,j++)
arr[i]=temp[j];
}
OutPut:
manoj@ubuntu:~/cp$ gcc merge_sort.c -o merge_sort
manoj@ubuntu:~/cp$ ./merge_sort
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = = = = = = = Merge Sort in C = = = = = = = = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 7
Enter 7 integers into the array:
4
5
35
2
5
1
35
Before sorting the array elements are:
4 5 35 2 5 1 35
After sorting the array elements are:
1 2 4 5 5 35 35
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
void mergesort(int arr[], int i, int j);
void merge(int arr[], int i1, int j1, int i2, int j2);
int main()
{
int arr[100], n, i, j;
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\n= = = = = = = = = = = Merge Sort in C = = = = = = = = = = = =");
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
mergesort(arr,0,n-1);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void mergesort(int arr[],int p,int q)
{
int mid;
if(p<q)
{
mid=(p+q)/2;
mergesort(arr,p,mid);
mergesort(arr,mid+1,q);
merge(arr,p,mid,mid+1,q);
}
}
void merge(int arr[], int i1, int j1, int i2, int j2)
{
int temp[100];
int i,j,k;
i=i1;
j=i2;
k=0;
while(i<=j1 && j<=j2)
{
if(arr[i]<arr[j])
{
temp[k]=arr[i];
i++;
}
else
{
temp[k]=arr[j];
j++;
}
k++;
}
while(i<=j1)
{
temp[k]=arr[i];
k++;
i++;
}
while(j<=j2)
{
temp[k]=arr[j];
k++;
j++;
}
for(i=i1,j=0; i<=j2; i++,j++)
arr[i]=temp[j];
}
OutPut:
manoj@ubuntu:~/cp$ gcc merge_sort.c -o merge_sort
manoj@ubuntu:~/cp$ ./merge_sort
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = = = = = = = Merge Sort in C = = = = = = = = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 7
Enter 7 integers into the array:
4
5
35
2
5
1
35
Before sorting the array elements are:
4 5 35 2 5 1 35
After sorting the array elements are:
1 2 4 5 5 35 35
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$