Showing posts with label Program - Simple Merge Sort. Show all posts
Showing posts with label Program - Simple Merge Sort. Show all posts

Saturday 31 March 2012

Program - Simple Merge Sort


Program - Simple Merge  Sort

// Simple Merge sort

#include<stdio.h>
#include<conio.h>

int merge_sort(int,int *,int,int*,int*);
int bubble_sort(int,int*);

int bubble_sort(int n,int l[])
{
        int flag=1;
        int i,j,k,temp;
        for(j=0;j<n-1;j++)
        {
                for(k=0;k<n-j-1;k++)
                {
                        if(l[k]>l[k+1])
                        {
                                temp=l[k];
                                l[k]=l[k+1];
                                l[k+1]=temp;
                                flag=0;
                        }
                }
                if(flag)
                        break;
                else
                        flag=1;
        }
        printf("\nEntered list in Ascending Order is as follows:");

        for(i=0;i<n;i++)
                printf("\n %d",l[i]);
        return 0;
}

int merge_sort(int n,int list_a[],int m,int list_b[],int result_list[])
{
        int i=0;
        int j=0;
        int k=0;
        int ch,l;
        while((i<n)&&(j<m))
        {
                if(list_a[i]<list_b[j])
                {
                        result_list[k]=list_a[i];
                        i++;
                        k++;
                }
                else
                        if(list_a[i]>list_b[j])
                        {
                                result_list[k]=list_b[j];
                                j++;
                                k++;
                        }
                        else
                        {
                                result_list[k]=list_a[i];
                                i++;
                                j++;
                                k++;
                        }
                printf("\n");
                for(ch=0;ch<k;ch++)
                        printf("\n %d",result_list[ch]);

        }
        if(i<n)
        {
                for(l=i;l<n;l++)
                {
                        result_list[k]=list_a[i];
                        i++;
                        k++;
                        printf("\n");
                        for(ch=0;ch<k;ch++)
                                printf("\n %d",result_list[ch]);
                }
        }
        else
                if(j<m)
                {
                        for(l=j;l<m;l++)
                        {
                                result_list[k]=list_b[j];
                                j++;
                                k++;
                                printf("\n");
                                for(ch=0;ch<k;ch++)
                                        printf("\n %d",result_list[ch]);
                        }
                }
        return(k);
}

void main()
{
        int list_a[100],list_b[100];
        int result[200];
        int n,m,k,i;
        clrscr();
        printf("Input the number of elements of list_a : ");
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
                printf("\nInput the elements: %d : ",i+1);
                scanf("%d",&list_a[i]);
        }

        bubble_sort(n,list_a);

        printf("\n\nInput the number of elements of list_b:");
        scanf("%d",&m);

        for(i=0;i<m;i++)
        {
                printf("\nInput the elements: %d:",i+1);
                scanf("\n %d",&list_b[i]);
        }
        bubble_sort(m,list_b);

        k=merge_sort(n,list_a,m,list_b,result);

        printf("\nDuplicates are: %d",m+n-k);
        printf("\n");

        printf("\nSorted list is as follows:");
        for(i=0;i<k;i++)
        {
                printf("\n %d",result[i]);
        }
        getch();
}


Posted By : Ruchita Pandya