# how to merge two arrays into 3rd array

 imran LP · March 5, 2015 i searched the internet . but couldn't really get it   if someone can explain me in a easy way then that would be of great help . i am actually a beginner , so i don't know much of advance level . so keep it easy !the program is like there is 1st array containing some data . lets take it - {10,20,30,40,50) and 2nd array be like - {48,35,26,18,9} now i need to store this is 3rd array and it should also be sorted in ascending order.

## Replies

 Dol Lod · March 5, 2015 There are a variety of different approaches you could take, but this is what I would do.1) Allocate a third array of the size of the first array and the second array. 2) Sort the two arrays individually in ascending order.3) Then merge the arrays together like you would in merge sort. Merge Sort Algorithm:void mergeArray(ItemType* array,int start,int size){if((size-start)<2)return;mergeArray(array,start,middle);int middle=(start+size)/2; // 2 4mergeArray(array,middle,size);int firstHalf=start;int secondHalf=middle;int mergedArrayPosition=0; int mergedArraySize=size-start;ItemType* mergedArray=(ItemType*)malloc(mergedArraySize*sizeof(ItemType));if(mergedArray==NULL)return;while(firstHalfarray[secondHalf]?array[secondHalf++]:array[firstHalf++]);while(firstHalfarray[secondHalf]?array[secondHalf++]:array[firstHalf++]);while(firstHalf c     for(int j=0;j<5;j++)     {         c[j]   = a[j];         c[j+5] = b[j];     }     //bubble sorting c     for(j=0;j<9;j++)         for(int k=1;k<10;k++)         {             if(c[k] < c[j])             {                 temp = c[j];                 c[j] = c[k];                 c[k] = c[j];             }         }}         ``what is wrong with site bbcode  ... I edited this post 6 times imran LP · March 6, 2015 thanks a lot everyone but this guy Mohamad Mirohamadi did it in a easy way that i can understand as compared to what i'v learnt so far . dude can you explain me how are we using c[j+5] where j is just executing it 5 times . i mean can you write the whole program . include printf n everything complete.? that would be of great help!! Mohamad Mirohamadi · March 7, 2015 j  just executing from 0 to 4  , so we add 5 unit to create 5 - 9 and fill c[5] to c[9] with b[0] to b[4]when j =0  then c[j+5] = = =  c[5]
• 1

## C

126,459 followers