answersLogoWhite

0


Best Answer

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

/*Merging of two arrays*/

#include"stdio.h"

#include"conio.h"

#include"process.h"

#define MAXELE 25

void merge(int a[],int b[],int c[],int n1,int n2,int n3)

{

int apoint,bpoint,cpoint;

int alimit,blimit,climit;

alimit=n1-1;

blimit=n2-1;

climit=n3-1;

if(n1+n2!=n3)

{

printf("\n\nArray boinds incompatible");

getch();

exit(1);

}

apoint=0;

bpoint=0;

cpoint=0;

for(;apoint<=alimit&&bpoint<=blimit;cpoint++)

if(a[apoint]<b[bpoint])

c[cpoint]=a[apoint++];

else

c[cpoint]=b[bpoint++];

while(apoint<=alimit)

c[cpoint++]=a[apoint++];

while(bpoint<=blimit)

c[cpoint++]=b[bpoint++];

}/*end merge*/

void main()

{

clrscr();

int a[MAXELE],b[MAXELE],c[MAXELE],i,n1,n2,n3;

printf("Enter number of elements for the 1st array...");

scanf("%d",&n1);

for(i=0;i<n1;i++)

{

printf("Enter value at a[%d]=",i);

scanf("%d",&a[i]);

}

printf("\n\nEnter number of elements for the 2nd array...");

scanf("%d",&n2);

for(i=0;i<n2;i++)

{

printf("Enter value at b[%d]=",i);

scanf("%d",&b[i]);

}

n3=n1+n2;

merge(a,b,c,n1,n2,n3);

printf("\n\nMerged 3rd array:\n");

for(i=0;i<n3;i++)

printf("c[%d]=%d\n",i,c[i]);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Create a new array, giving it a size that is enough to hold the combined array; copy all elements from the first array; copy all the elements from the second array.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you merge the contents of two sorted arrays into a new array such that the contents of this new array is sorted?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the use of auxiliary array in merge sort?

In merge sort the whole is divided into two sub arrays. (This way of solving problem is called Divide and conquer algorithm) These sub arrays are called auxiliary arrays. First an array A is divided into two auxiliary arrays A1 and A2. Now these auxiliary arrays are further divided until we reach a stage with an auxiliary array of 2 elements. These 2 elements are arranged in incremental order and merged with the previous divided arrays. So we can say that auxiliary array is used to implement the basic principle of merge sort.


How do you merge arrays in PHP?

To merge arrays in PHP all you have to do is use the array_merge() function like shown below: &lt;?php $array1 = array("Matt", "Michael", "Justin"); $array2 = array("Janice", "Janet", "Kylie"); $array3 = array_merge($array1, $array2); ?&gt; One thing to remember when merging arrays is you might be creating duplicate results, if you don't want 2 or more of the same result in the array remember to use the function array_unique() to remove the duplicate results from it!


How do you merge and sort an array using PHP?

To merge and sort an array in PHP you need to use the array_merge() and sort() functions like shown in the example below: &lt;?php $array1 = array(1, 5, 3, 9, 7); $array2 = array(8, 2, 6, 4, 0); // merge the arrays $merge = array_merge($array1, $array2); // 1, 5, 3, 9, 7, 8, 2, 6, 4, 0 // sort the array sort($merge); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ?&gt;


How many types of sorting array in C programming?

You can sort an array with any method you want, but there is a built-in qsort function, declared in stdlib.h (see the attached link).bubble sort, quick sort, insertion sort, merge sort, radix sort and lot more..merge sort is the most efficient one..


How do you sort a queue?

You don't. Queues are a first in, first out structure, specifically used to process incoming data in the same order it arrives. If you want to sort a data sequence then use an array or a list.


Is merge sort external sorting?

Can be. (Meaning: you can merge sorted files without loading them entirely into the main memory.)


How divide and conqure approach is used in merge sort?

The data to be sorted is broken into smaller and smaller chunks, then each individual chunk is sorted, then the chunks are merged together and sorted


What is the c plus plus programme for merge sort without recursion?

The following template function will sort an array of any type using the merge sort algorithm, non-recursively. Note that you must call the function by passing a pointer to the array, rather than passing the array by reference as you normally would with sorting algorithms. This is because the function uses a second array (a work array) of the same size to perform the actual merge, switching from one to the other upon each iteration, so there's no guarantee the final sorted array will be the one you originally referred to (it could be the work array). You could maintain a count of the iterations and perform an extra copy of the work array back to your referenced array if the count is odd, but it's more efficient to simply return the sorted array through a pointer. The code can be difficult to follow if you're not familiar with the merge sort algorithm, so I've commented the code verbosely to explain what's going on. In essence, we're treating the original array as being several subsets of 1 element each. A subset of 1 is already sorted, so we simply merge these subsets in pairs, so the new array contains sorted subsets of 2 elements each. We repeat the process, merging each pair of subsets to create sorted subsets of 4, then 8 and so on, until there is only 1 sorted subset in the array. When we merge two subsets into one, we simply compare the first element of each subset and place the lowest into the merged subset. Note that in the final iteration, the second subset of the pair may be smaller than the first, or it may not exist at all (in which case the remaining subset may be smaller). However, the merge algorithm caters for this eventuality quite efficiently. If there's only one subset remaining (which can happen during any iteration as elements are removed from the subsets and merged into the work array), the remaining elements are simply copied sequentially from that subset since they will already be in order (as determined by the previous iteration). template&lt;typename T&gt; void merge_sort(T* A[], size_t size) { // Arrays of length 0 or 1 are already sorted. if( size &lt; 2 ) return; // Instantiate a new array of the same size as A. T *B = new T[size]; // Array A initially contains subsets of width 1 element, then 2, 4, 8... for( size_t width=1; width&lt;size; width&lt;&lt;=1 ) { // Dereference A (now known as C). T *C = *A; // Array B initially holds subsets of width 2 elements, then 4, 8, 16... size_t width2 = 2*width; // Iterate through each pair of subsets in C... // sub1 is the start index of the first subset of the pair in C. for( size_t sub1=0; sub1&lt;size; sub1+=width2 ) { // sub2 is the start index of the second subset of the pair in C. size_t sub2 = min( sub1+width, size ); // next is the start of the next pair of subsets in C. size_t next = min( sub1+width2, size ); // Start with the first elements in each pair of subsets in C // (the ones with the lowest values in each subset). size_t c1 = sub1; size_t c2 = sub2; // Iterate through B's elements from index [sub1] to index [next-1]. for( size_t b=sub1; b&lt;next; ++b ) // Copy the lowest of the two indexed values in C to B. B[b] = ( c1&lt;sub2 &amp;&amp; ( c2&gt;=next C[c1]&lt;=C[c2] )) ? C[c1++] : C[c2++]; } // Swap the roles of *A and B (note that C still points to the dereferenced A at this point). *A=B; B=C; } // Finished with B (*A is now the fully-sorted array). delete[]B; }


How do you merge two array without using function?

Take another array big enough to hold both array copy content of these two array into new one. You merged two array and haven't used a single function.!


Which key element will be best for merge sort and why?

There is no key element in a merge sort. Unlike quick sort which requires a key element (a pivot) to recursively divide a subset into two subsets, merge sort simply divides a subset into subsets of 1 element and merges adjacent subsets together to produce sorted subsets. When there is only one subset remaining, the subset is fully sorted.


What are advantages and disadvantages of merging?

merge sort is the most efficient way of sorting the list of array.


Which algorithm is more efficient- insertion sort algorithm or merge sort algorithm?

On average merge sort is more efficient however insertion sort could potentially be faster. As a result it depends how close to reverse order the data is. If it is likely to be mostly sorted, insertion sort is faster, if not, merge sort is faster.