Showing posts with label unsorted array as input. Show all posts
Showing posts with label unsorted array as input. Show all posts

Thursday, 25 January 2018

MERGE and SORT CPP PROGRAMMING

Program:

#include<iostream>
#define size 1000
using namespace std;
int size1;
int* bubble(int d[],int n)
{
    int i=0,j=0;
    for(i=0;i<n;i++)
        {
            for(j=0;j<(n-i-1);j++)
            {
                if(d[j]>d[j+1])
                {
                    int t=d[j+1];
                    d[j+1]=d[j];
                    d[j]=t;
                }
            }
        }
        return d;
    }
int* merge1(int a[],int const h1,int b[],int const  h2)
{
    static int c[size];
    int i=0,j=0;
    for(i=0;i<h1;i++)
    {
        c[i]=a[i];
    }
    for(j=0;j<h2;j++)
    {
        c[i]=b[j];
        i++;
    }
    size1=h1+h2;
        return c;
}

void disp(int a[],int len)
{
    for(int i=0;i<len;i++)
        cout<<a[i]<<" ";
    cout<<endl;
}


int main()
{int n1,n2;
cout<<"Enter the size of array1 and array2:";
cin>>n1>>n2;
int *a=new int[n1];
int *b=new int[n2];
cout<<"Enter "<<n1<<" elements of array1:";
for(int i=0;i<n1;i++)
{
    cin>>a[i];
}
cout<<"Enter "<<n2<<" elements of array2:";
for(int i=0;i<n2;i++)
{
    cin>>b[i];
}
int *c=merge1(a,n1,b,n2);
int *d=bubble(c,size1);
cout<<"The merge_sort array is:";
disp(d,size1);

}

Output:
Enter the size of array1 and array2: 5 2
Enter 5 elements of array1: 0 5 20 15 30
Enter 2 elements of array2: 10 35

The merge_sort array is: 0 5 10 15 20 30 35

Wikipedia

Search results