What do you mean by Merge sort in data structure
Hello Guys, I am having a problem in the merge sort in data structure. I tried it practically but I am not able to execute the code as it is having lots of bugs in the program. I tried to correct the code I find a new bug in it. I have neither understood the concept nor practically. If anyone having any information or knowledge about this query then please let me know soon as possible.
Re: What do you mean by Merge sort in data structure
Merging means combining two sorted list. For this the elements from both the sorted list are compared. The smaller of both the elements is then stored in the third array. The sorting is complete when all the elements from both the lists are placed in the third list. You must not make a mistake by comparing it only from one side. It must be compared from both the sides. After this you have to compare both the list again and re arrange it. Merging sort is easy to use to when there are big list of elements then it becomes bit tough.
Re: What do you mean by Merge sort in data structure
Code:
Declare array AA , BB and CC
store elements in to array AA and BB
set i=j=k=0 // i for AA j for BB and k for CC
Repeat till i<=length of array CC-1
check if AA[i] <=BB[j]
CC[k]= AA[i]
++k;++i;
else
CC[k]= BB[j]
++k;++j;
repeat step 6 to 10
End of loop
for ( i = j = k = 0 ; i <= 9 ; )
{
if ( a[j] <= b[k] )
c[i++] = a[j++] ;
else
c[i++] = b[k++] ;
if ( j == 5 || k == 5 )
break ;
}
for ( ; j <= 4 ; )
c[i++] = a[j++] ;
for ( ; k <= 4 ; )
c[i++] = b[k++] ;
This is an algorithm of merge sort which is used in sorting operation. The sorting process in this type is not that difficult as compare to other sorting but it gets bit difficult when there is a large amount of elements in the list and it becomes more tougher when you are not aware of the concept.
Re: What do you mean by Merge sort in data structure
Code:
#include <stdio.h>
#include <conio.h>
void main( )
{
int a[5] = { 11, 2, 9, 13, 57 } ;
int b[5] = { 25, 17, 1, 90, 3 } ;
int c[10] ;
int i, j, k, temp ;
clrscr( ) ;
printf ( "Merge sort.\n" ) ;
printf ( "\nFirst array:\n" ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", a[i] ) ;
printf ( "\n\nSecond array:\n" ) ;
for ( i = 0 ; i <= 4 ; i++ )
printf ( "%d\t", b[i] ) ;
for ( i = 0 ; i <= 3 ; i++ )
{
for ( j = i + 1 ; j <= 4 ; j++ )
{
if ( a[i] > a[j] )
{
temp = a[i] ;
a[i] = a[j] ;
a[j] = temp ;
}
if ( b[i] > b[j] )
{
temp = b[i] ;
b[i] = b[j] ;
b[j] = temp ;
}
}
}
This is the program where in it shows the execution of merge sort. It is explained step wise so that it becomes quite easy for you to understand. If you look at the program then you will come to know that how the arrays are declared and then how it is stored and the further process. Well, you must execute it and see it which will be more easy to understand.