I was asked this in an interview related to C#. There are 2 arrays - presorted from lowest to highest. I need put all of these combined in a 3rd array and they should be inserted in a sorted manner (as they are going in).
The solution I mentioned was as below:
For argument sake lets say
Array 1has the following elements -1,2,3,4,5andArray 2has the following elements -6,7,8,9,10Since the 2 arrays are pre-sorted - you would compare the first element of
Array 1to the first element ofArray 2and the insert the lower element inArray 3.You would then do the same for
Element 2ofArray 1andElement 1ofArray 2and pop in the next smallest number
The approach which I mention should work - but the questions I have are as follows:
- Is this the most efficient approach?
- Are there technical term (like Binary Search Algo etc etc) which can describe this process?
- Any other pointers for this problem on solving?
