Reputation: 550
Given two arrays, arr1 and arr2, that have been sorted in descending order, output an array which appends the values from both arr1 and arr2 while being sorted in descending order.
MyApproach
I found 2 -3 links helpful and I noticed that I have a similar kind of code here.But I am unable to get the expected output.
One of my Resources:How to merge two sorted arrays into a sorted array?
Can Anyone guide me what I am doing wrong.
I have made the code for length of array1 >= length of array2 for other part I will append later
public int[] join(int[] arr1,int[] arr2)
{
int l1=arr1.length;
int l2=arr2.length;
int i=0,j=0,k=0;
int c[]=new int[arr1.length+arr2.length];
if(l1>=l2)
{
for(;i<arr1.length;)
{
if(arr1[i]>=arr2[j])
{
c[k]=arr1[i++];
System.out.println(c[k]);
k++;
}
else if(arr2[j]>arr1[i])
{
c[k]=arr2[j++];
System.out.println(c[k]);
k++;
}
}
//System.out.println(j);
//System.out.println(i);
while(i<arr1.length)
{
c[k]=arr1[i++];
System.out.println(c[k]);
k++;
}
while(j<arr2.length)
{
c[k]=arr2[j++];
System.out.println(c[k]);
k++;
}`
}
return c;
//write your code here
}
Parameters ActualOut Expected Out
{100,90,80,70,60}
null {105,100,95,90,85,80,75,70,65,60} {105,95,85,75,65}
Upvotes: 3
Views: 64
Reputation: 1117
While I think Elliot's answer is the best way to go, I have some suggestions for how to stick with your code and make it work.
First, in your for
loop towards the start of your function, have the condition be i < l1 && j < l2
, instead of only checking i
(also, if you're going to declare variables for the arrays' lengths, use them!). This allows you to get rid of the if (l1 >= l2)
check that you have, making it work regardless of which array is longer.
Otherwise, your code seems fully functional. One additional tip that I would suggest would be to make your join
method static
. This allows you to call it independently, by simply calling join (someArr1, someArr2)
instead of SomeObject.join(someArr1, someArr2)
(since all of the variables used in join
are local, not instance variables of SomeObject
.
Upvotes: 2
Reputation: 201477
This may be the best argument in favor of the conditional operator ? :
(a ternary operation). To your question, you need to guard against null
input. Then you could take advantage of System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
to copy the remaining elements after you loop. Something like
public static int[] join(int[] arr1, int[] arr2) {
if (arr1 == null) {
return arr2;
} else if (arr2 == null) {
return arr1;
}
int p = 0, i = 0, j = 0;
int[] out = new int[arr1.length + arr2.length];
while (i < arr1.length && j < arr2.length) {
out[p++] = (arr1[i] > arr2[j]) ? arr1[i++] : arr2[j++];
}
if (i < arr1.length) {
System.arraycopy(arr1, i, out, p, arr1.length - i);
} else if (j < arr2.length) {
System.arraycopy(arr2, j, out, p, arr2.length - j);
}
return out;
}
Which I tested like
public static void main(String args[]) {
int[][] tests = { { 3, 2, 1 }, { 10, 9, 7 }, { 6, 5, 4 } };
System.out.println(Arrays.toString(join(tests[0], join(tests[1], tests[2]))));
}
And, I got
[10, 9, 7, 6, 5, 4, 3, 2, 1]
Upvotes: 2