How different would it be to merge a collection of unsorted arrays type Comparable versus the code below?
Comparable [][] collections {colA, colB, colC};
After searching I found a way of merging two sorted array of primitive data.
public static int[] merge(int[] a, int[] b) {
int[] answer = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length)
{
if (a[i] < b[j])
{
answer[k] = a[i];
i++;
}
else
{
answer[k] = b[j];
j++;
}
k++;
}
while (i < a.length)
{
answer[k] = a[i];
i++;
k++;
}
while (j < b.length)
{
answer[k] = b[j];
j++;
k++;
}
return answer;
}
Here, How to merge two sorted arrays into a sorted array?
Assuming that I have K arrays (e.g. A & B...), and I want to obtain list C (allowing duplicate items). I want to only compare each element one time. For faster performance even if I compromise on memory.
Thank you!