Merge sort in CPP

  • Merge sort coding in CPP
  • //plz insert the tags of header libraries by your own
    void merge(int low,int mid,int high);
    void mergesort(int low,int high);
    int a[1000],count;
    void main()
    {
    //	clrscr();
    	int low,high;
    	for(int i=0;i<10;i++)
    	{
    		a[i]=rand();
    	}
    	low=0;
    	high=9;
    	mergesort(low,high);
    
           //	a[0]=2;a[1]=5;a[2]=8;a[3]=11;a[4]=14;a[5]=3;a[6]=4;a[7]=9;a[8]=10;a[9]=15;
           //	merge(0,4,9);
    	for(int j=0;j<10;j++)
    	{
    		cout<<a[j]<<"\t";
    	}
    	cout<<"\ncomplexity is"<<count;
    	getch();
    
    }
    void mergesort(int low,int high)
    {
    	int mid;
    	if(low<high)
    	{
    		mid=floor((low+high)/2);
    		mergesort(low,mid);
    		mergesort(mid+1,high);
    		merge(low,mid,high);
    	}
    }
    void swap(int &a,int &b)
    {
    	int temp;
    	temp=a;
    	a=b;
    	b=temp;
    	count++;
    }
    
    void merge(int low,int mid,int high)
    {
    	int l=0,m=0,b[1000],c[1000];
    	for(int j=low;j<=mid;j++)
    		b[j-low]=a[j];
    	for(j=mid+1;j<=high;j++)
    		c[j-mid-1]=a[j];
    	l=mid-low+1;
    	m=high-mid;
    	int i,k;
    	for(j=0,i=0,k=low;i<l&&jc[j])
    		{
    			a[k]=c[j];
    			j++;
    		}
    		else
    		{
    			a[k]=b[i];
    			i++;
    		}
    	}
    	if(i==l)
    	{
    		for(;k<l+m;k++,i++)
    		{
    			a[k]=c[j];
    		}
    	}
    	else
    	{
    		for(;k<l+m;j++,k++)
    		{
    			a[k]=b[i];
    		}
    	}
    }
    
    
    
    
  • STA
    //plz insert the tags of header libraries by your own
    
    int a[100];
    void mergesort(int low,int high);
    void merge(int a,int b,int c);
    void main()
    {
    	int input,i=0,j=0,w;
    	cout<>input;
     int lo=0;
    	while(i>a[i];
    		i++;
    
    	}
    	cout<<"inputed element=";
    	while(j<input)
    	{
    		cout<<a[j];
    		j++;
    	}
    	mergesort(lo,input);
    	while(w<input)
    	{
    		cout<<a[w];
    		w++;
    	}
    
    }
    void mergesort(int low,int high)
    {
    	int mid;
    	if(low<high)
    	{
    		mid=(low+high)/2;
    		mergesort(low,mid);
    		mergesort(mid+1,high);
    		merge(low,mid,high);
    	}
    }
    void merge(int l,int b,int c)
    {
    	int m=0,d=0,e=0,h,u=0;
    	/*while(l<b)
    	{
    		sta[l]=a[l];
    		l++;
    	}
    	k=b+1;
    	while(k<c)
    	{
    		sta1[m] = a[k];
    		m++;
    		k++;
    	}
    	*/
    	e=b+1;
    	for(h=0;h<c;h++)
    	{
    		if(a[d]a[e+1])
    				{
    					a[u]=a[e+1];
    					u++;
    				}
    				else
    					a[u]=a[e];
    				u++;
    			}
    		}
    		else
    		{
    			a[u]=a[e];
    			e++;
    			u++;
    		}
    	}
    }
    
    
    
  • Leave a Reply

    Please log in using one of these methods to post your comment:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s