#include <stdio.h>

#define ArrLeng1  9
#define ArrLeng2 12

// Merge two sorted array into one sorted array
void main()
{
	int arr1[]={2,4,8,9,15,23,26,37,91} ;
	int arr2[]={1,2,6,11,12,18,25,38,42,93,97,342} ;
	int arrRes[ArrLeng1+ArrLeng2];
	int ind , i ,j ;

	ind=i=j=0 ;
	while( (i<ArrLeng1) && (j<ArrLeng2) )
	{
	  if (arr1[i] > arr2[j])
	  {
		  arrRes[ind]=arr2[j] ;
		  ind++ ;
		  j++ ;
	  }
	  else
	  {
		  arrRes[ind]=arr1[i] ;
		  ind++ ;
		  i++ ;
	  }
	}
	if (i<ArrLeng1)
	{	
	  for(i;i<ArrLeng1;i++)
	  {
		  arrRes[ind]=arr1[i];
		  ind++;
	  }
	}
	else
	{
	  for(j;j<ArrLeng2;j++)
	  {
		  arrRes[ind]=arr2[j];
		  ind++;
	  }

	}
	for (i=0;i<(ArrLeng1+ArrLeng2);i++)
		printf("%d ",arrRes[i]);
	printf("\n");
}
Published in: C++
Download

Related snippets