CREATE OWN LIBRARY

Merge two Array and stored in the third Array

Back to Programming

Description

The elements of the two arrays are taken as the input. Then the two arrays are merged together and stored in the third array

 

 

 

Algorithm

INPUT: Array elements
OUTPUT: the new array after merging
PROCESS:
Step 1: [taking the input]
	Read m [number of elements of the 1st array]
	For i=0 to m-1 repeat
		Read a[i]
	[end of ‘for’ loop]
	Read n [number of elements of the 2nd array]
	For i=0 to n-1 repeat
		Read b[i]
	[End of ‘for’ loop]
Step 2: [merging the two arrays]
	For i=0 to m-1 repeat
		Set c[i]<-a[i]
	[End of ‘for’ loop]
	Set j<-i
	For i=0 to n-1 repeat
		Set c[j]<-b[i]
		Set j<-j+1
	[End of ‘for’ loop]
	[printing the merged array]
	For i=0 to m+n-1 repeat
		Print c[i]
	[End of ‘for’ loop]
Step 3: Stop

Code

Complexity:

The time complexity to take the number of elements is O(n) and the time complexity to merge is  O(m + n) where m and n the number of elements of the two arrays respectively.