MERGE SORT

 MERGE SORT:-

ALGORITHM:-


STEP 1. SET L:=1 

STEP 2. REPEAT STEP 3 TO 6 WHILE L<N;

STEP 3. CALL MERGEPASS (A,N,L,B)

STEP 4. CALL MERGEPASS(B,N,2*L,A)

STEP 5. SET L:=4*L

STEP 6. EXIT


CODE:-


#include<stdio.h>

#include<conio.h>

int main()

{

  int i,j,n1,n2,a1[20],a2[20],m[20];

  clrscr();

  printf("enter size of first array : ");

  scanf("%d",&n1);

   printf("\n");

    for(i=0;i<n1;i++)

{

printf("enter array element : ");

scanf("%d",&a1[i]);

}

 printf("\n");

    printf("enter size of second array : ");

    scanf("%d",&n2);

    printf("\n"); 

    for(i=0;i<n2;i++)

{

printf("enter array element : ");

scanf("%d",&a2[i]);

}

          printf("\n\n the first array : ");

          for(i=0;i<n1;i++)

{

printf("%d  ",a1[i]);

}

         printf("\n\n the second array : ");

         for(i=0;i<n2;i++)

{

printf("%d  ",a2[i]);

}

        for(i=0;i<n1;i++)

{

m[i]=a1[i];

}

        for(i=n1;i<n1+n2;i++)

{

m[i]=a2[i-n1];

}

printf("\n\n merged array : ");

       for(j=0;j<n1+n2;j++)

   {

   printf("%d  ",m[j]);

   }

    getch();

    return 0;

}


Comments

Popular posts from this blog

AI Lab file 2024

AI Prolog PDF