SHORTING USING MERGE SORT

828

SHORTING USING MERGE SORT

DATA Structure

BCA Third Semester

 

#include<stdio.h>
#include<conio.h>
void merge(int [],int ,int ,int );
void break_a(int [],int ,int );
int main()
{
int arr[30];
int i,size;
clrscr();
printf(“\n\t——- Merge sorting method ——-\n\n”);
printf(“Enter total no. of elements : “);
scanf(“%d”,&size);
for(i=0; i<size; i++)
{
printf(“Enter %d element : “,i+1);
scanf(“%d”,&arr[i]);
}
break_a(arr,0,size-1);
printf(“\n\t——- Merge sorted elements ——-\n\n”);
for(i=0; i<size; i++)
printf(” %d\n”,arr[i]);
getch();
return 0;
}

void break_a(int arr[],int min,int max)
{
int mid;
if(min<max)
{
mid=(min+max)/2;
break_a(arr,min,mid);
break_a(arr,mid+1,max);
merge(arr,min,mid,max);
}
}

void merge(int arr[],int min,int mid,int max)
{
int tmp[30];
int i,j,k,m;
j=min;
m=mid+1;
for(i=min; j<=mid && m<=max ; i++)
{
if(arr[j]<=arr[m])
{
tmp[i]=arr[j];
j++;
}
else
{
tmp[i]=arr[m];
m++;
}
}
if(j>mid)
{
for(k=m; k<=max; k++)
{
tmp[i]=arr[k];
i++;
}
}
else
{
for(k=j; k<=mid; k++)
{
tmp[i]=arr[k];
i++;
}
}
for(k=min; k<=max; k++)
arr[k]=tmp[k];
}

OUTPUT
——- Merge sorting method ——-

Enter total no. of elements : 5
Enter 1 element : 40
Enter 2 element : 10
Enter 3 element : 30
Enter 4 element : 60
Enter 5 element : 20

——- Merge sorted elements ——-
10
20
30
40
60

 

 

if you need a Downloadable link once message us on-page you can get at a time.

m.me/bcanotesnepal

This post is posted for not further third-party purposes.

Copyright By:-  Bcanotesneapl

Not allowed to upload these notes to other sites.

Like our Facebook Page https://www.facebook.com/bcanotesnepal/

if you want to download this PDF comment on us we will provide you a downloadable link.

if you like please share this post with your friend who studies in BCA

if you have any query please comment below,

if you like our sites. you can visit again for the latest updates notes of different BCA subjects of Presentation and different topics with assignment questions and solutions.

Don’t forget to like our page and post also share with your friend and having your group.

if you have any questions you can drop or share them with me using the comment box I will try to answer your question.

Like our Facebook Page https://www.facebook.com/bcanotesnepal/

LEAVE A REPLY

Please enter your comment!
Please enter your name here