how to merge two arrays into 3rd array

0 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. 

Post a Reply

Replies

Oldest  Newest  Rating
+1 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 4
mergeArray(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(firstHalf<middle && secondHalf<size)
mergedArray[mergedArrayPosition++]=(array[firstHalf]>array[secondHalf]?array[secondHalf++]:array[firstHalf++]);
while(firstHalf<middle)
mergedArray[mergedArrayPosition++]=array[firstHalf++];
while(secondHalf<size)
mergedArray[mergedArrayPosition++]=array[secondHalf++];
for(mergedArrayPosition=0;mergedArrayPosition<mergedArraySize;mergedArrayPosition++)
array[start+mergedArrayPosition]=mergedArray[mergedArrayPosition];
free(mergedArray);
}

Now the third part of what I'm describing is  using

while(firstHalf<middle && secondHalf<size)
mergedArray[mergedArrayPosition++]=(array[firstHalf]>array[secondHalf]?array[secondHalf++]:array[firstHalf++]);
while(firstHalf<middle)
mergedArray[mergedArrayPosition++]=array[firstHalf++];
while(secondHalf<size)
mergedArray[mergedArrayPosition++]=array[secondHalf++];
for(mergedArrayPosition=0;mergedArrayPosition<mergedArraySize;mergedArrayPosition++)
array[start+mergedArrayPosition]=mergedArray[mergedArrayPosition];

to merge the two arrays together and return it. 
+1 c student · March 5, 2015
cos qsort has a worse time complexity meaning it requires modification to its naive form to make it effective whereas merge sort has a more well-rounded time complexity.

if you're going for speed, quicksort would probably be your choice however you will need to add some modifications into the selection of the pivot such as medians e.g. median of the first, middle and last element will give a better middle value.
+1 Mohamad Mirohamadi · March 5, 2015
Hey Imran this is the easy way for you:


void main()
{
     int a[] = {10,20,30,40,50}, b[] = {48,35,26,18,9};
     int c[10], temp;
     
     //copying a&b --> 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
0 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!!
0 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

107,336 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator