Saturday, 1 June 2019

Merge Sort program in C

#include <stdio.h>
#include <stdlib.h>
void msort(int a[],int low ,int high,int n)
{
if(low < high)
{
int mid = (low+high)/2;
msort(a,low,mid,n);
msort(a,mid+1,high,n);
int i=low,j=mid+1,k=low;
int temp[n];
while(i<= mid && j <= high)
{
if(a[i] < a[j])
{
temp[k++] = a[i++];
}
else
{
temp[k++] = a[j++];
}
}
while(i <= mid)
{
temp[k++] = a[i++];
}
while(j<=high)
{
temp[k++] = a[j++];
}
for(i=low;i<=high;i++)
a[i]=temp[i];
}
}
main()
{
int n;
scanf("%d",&n);
int *a,i;
a=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
msort(a,0,n-1,n);
for(i=0;i<n;i++)
printf("%d ",a[i]);

}
================Merge sort on Array of Strings=================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void msort(char a[][100],int low ,int high,int n)
{
if(low < high)
{
int mid = (low+high)/2;
msort(a,low,mid,n);
msort(a,mid+1,high,n);
int i=low,j=mid+1,k=low;
char temp[n][100];
while(i<= mid && j <= high)
{
if(strcmp(a[i], a[j])<0)
{
strcpy(temp[k],a[i]);
k++;
i++;
}
else
{
strcpy(temp[k],a[j]);
k++;
j++;
}
}
while(i <= mid)
{
strcpy(temp[k],a[i]);
k++;
i++;
}
while(j<=high)
{
strcpy(temp[k],a[j]);
k++;
j++;
}
for(i=low;i<=high;i++)
strcpy(a[i],temp[i]);
}
}
main()
{
int n;
scanf("%d",&n);
int i;
char a[n][100];
for(i=0;i<n;i++)
scanf("%s",&a[i]);
msort(a,0,n-1,n);
for(i=0;i<n;i++)
printf("%s ",a[i]);
}
===========Merge Sort Simple Program Below===================================

#include <stdio.h>
#define MAX 100

int c[MAX];

merge(int a[], int ,int ,int , int );
mergesort(int a[], int ,int);

main()
{
                int n,i,a[MAX];
                printf("enter n value");
                scanf("%d",&n);
                for(i=0;i<n;i++)
                                scanf("%d",&a[i]);
                mergesort(a,0,n-1);
                for(i=0;i<n;i++)
                                printf("%d\t",a[i]);
}
mergesort(int a[],int low, int high)
{
                int mid;
                if(low<high)
                {
                                mid=(low+high)/2;
                                mergesort(a,low,mid);
                                mergesort(a, mid+1,high);
                                merge(a,low,mid,mid+1,high);
                }
}

/* merge two subparts obtained by dividing the array into 2 halves (low1 to high1 and low2 to high2)*/
merge(int a[], int low1,int high1,int low2, int high2)
{
                int i,j,k;
                i=low1;
                j=low2;
                k=low1;    /*after merging, lower index of merged array begins with low1,ends with high2*/
                while(i<= high1 && j<=high2)
                {
                                while(a[i]<=a[j] && i <=high1)
                                {
                                                c[k++]=a[i++];
                                }
                                while(a[j]<a[i]&& j <= high2)
                                {
                                                c[k++]=a[j++];
                                }
                }
                while(i<=high1)    /* if i <= high1 means there are elements still in subpart1 (low1 to high1)*/
                {
                                c[k++]=a[i++];  
                }
                while(j<=high2)
                {
                                c[k++]=a[j++];                  
                }
                for(k=low1;k<=high2;k++)  /* the sorted sub array in 'c' is written back to original array 'a'*/
                {
                                a[k]=c[k];
                }             
}

No comments:

Post a Comment