0% found this document useful (0 votes)
96 views

Merge Sort

The document describes a C program to implement merge sort to sort an array of integers in ascending order. It includes functions for merge sort, which recursively splits the array in half and then merges the sorted halves, and for merging the sorted halves. The program takes user input of the number of elements and values, calls the merge sort function, and prints the sorted output array.

Uploaded by

gouse1210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Merge Sort

The document describes a C program to implement merge sort to sort an array of integers in ascending order. It includes functions for merge sort, which recursively splits the array in half and then merges the sorted halves, and for merging the sorted halves. The program takes user input of the number of elements and values, calls the merge sort function, and prints the sorted output array.

Uploaded by

gouse1210
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Name of the Program:week7b

Week 7: Write C programs for implementing the following sorting methods to arrange a list of
integers in Ascending order : a) Merge sort
#include<stdio.h>
}
int a[100]; //globally declaring array
else
void merge (int a[],int low,int high,int mid);
{
void mergesort (int a[],int low,int high);
c[k]=a[j];
void main()
j++;
{
k++;
int i,n;
}
printf("\n enter the no. of elements");
}
scanf("%d",&n);
while(i<=mid)
printf("enter elements of list");
{
for(i=0;i<n;i++) //entering loop//
c[k]=a[i];
scanf("%d",&a[i]);
i++;
mergesort (a,0,n-1);//calling function//
k++;
printf("\n sorted array\n");
}
for(i=0;i<n;i++)//entering loop//
while(j<=high)
printf("%d\n",a[i]);
{
}
c[k]=a[j];
void mergesort(int a[100],int low,int high)
j++;
{
k++;
int mid;
}
if(low<high)//checing condition//
for(i=0;i<k;i++)
{
a[i]=c[i];
mid=(low+high)/2;
mergesort(a,low,mid);
}
mergesort(a,mid+1,high);
OUTPUT:
merge(a,low,high,mid);
$ ./a.out
}
enter the no. of elements6
}
enter elements of list
void merge(int a[100],int low,int high,int
23
mid) //called function//
12
{
56
int i,j,k,c[50];
34
i=low;
67
j=mid+1;
45
k=low;
sorted array
while((i<=mid)&&(j<=high))
12
{
23
if(a[i]<a[j])
34
{
45
c[k]=a[i];
56
i++;
67
k++;
[13r21a0507@mysqldbserver week7]$

You might also like