+ 1
Can u help me showing how can I merge two sorted arrays into one sorted array without using sort method.
Inputs are in ascending order. I need to create a method called "public static int [] mergeSorted ( int [] a , int [] b ) .. and then test this method ?
7 Antworten
+ 3
Algorithme:
a1 = initialize array 1
a2 = initialize array 2
a3 = declare array3 of size ( size a1 + size a2 ) # size is the count/number of element
c1 = 0 # initialize counter for index array 1
c2 = 0 # initialize counter for index array 2
c3 = 0 # initialize counter for index array 3
WHILE ( c3 < size c3 )
{
IF ( a1[c1] < a2[c2] ) # condition can be strict or not as needed for the case of equality
THEN
{
a3[c3] = a1[c1]
c1++
}
ELSE
{
a3[c3] = a2[c2]
c2++
}
c3++
}
So at the end c3 contains c1+c2 in ascending order...
Implementation:
You're kidding :D ?
Come on ! Go to practice :)
+ 1
What i have written is
Int [] c = new int [a.length + b.length] ;
Int i = 0, j = o, k = 0;
While ( i < a.length && j < b.length )
{
If ( a [i ] < b [ j ] )
{
c [k] = a [ i ]
i++;
}
else
{
c [ k ] = b [ j ]
j++;
}
k++;
}
while ( i < a.length )
{
c [ k ] = a [ i ]
i++;
k++;
}
While ( j < b.length )
{
c [ k ] = b [ j ] ;
j++;
k++;
}
return c ;
}
+ 1
You did implement my algorithm?
It don't seems... or you've missunderstood it ;)
In addition, your code is not valid and not complete: I work on your solution, but since, try to correct/complete what you write ( work in code playground, and save it as public, so you can test it, and I will know wher find it ), with or without implementing my algorithm ( the important is it's give the right result ).
Be carefull of missing semi-colon, and case sensitivity...
And notice, for my algorithm, is presupposed that the 2 startings arrays are in right order ^^ So if you don't want this behaviour, we need to change the algorithme, and maybe your code, when debugged/completed, will work nicer as my algo' ^^
+ 1
Yes, you're almost right: when debugged/completed your code works, but only if the two input arrays are sorted ^^
Either you have initials ordered data, either you must call Arrays.sort() for each... But maybe the constraint of not use the sort() function is restricted to the function?
For your additional question, it's probably almost the same, but using different types of variables... ( in fact I have an very old background of code, but known java only for very recently, so I need to much searches at references at each times :P )
+ 1
Thank you for your help man. I ll try to do this part too 😁😁
I am also beginner in java and programming but will find a way 😜
+ 1
"I am also beginner in java and programming but will find a way 😜"
Yeah!!!
It's a very good state of mind guy :)
0
Yeah i m sorry i missed some semicolons but i fixed it and its working now .. i used my algorithm and its fine man .. thanks a lot for your help .. but can i ask u for sth else .. what if we have now array lists and not arrays ? .. the question is the same 😁 .. and the method that should be implemented and tested is " public static ArrayList<Integer> mergeSorted ( ArrayList<Integer> a , ArrayList< Integer > b )