Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MergedArrays {
- public static void main(String[] args) {
- int[] array1={2,4,5,6,7};
- int[] array2={1,3,8,9,10};
- int[] array3=new int[array1.length+ array2.length];
- array3= merge(array1, array2);
- for (int e: array3)
- System.out.print(e+ " ");
- }
- public static int[] merge(int[] arr1, int[] arr2){
- int counter=0;
- int countArr1=0;
- int countArr2=0;
- int[] merged= new int[arr1.length+arr2.length];
- while (counter<(arr1.length+arr2.length)){
- if (arr1[countArr1]<arr2[countArr2]){
- merged[counter]= arr1[countArr1];
- counter++;
- countArr1++;
- if (countArr1>=arr1.length){
- while (countArr2< arr2.length) {
- merged[counter]= arr2[countArr2];
- counter++;
- countArr2++;
- }
- }
- }
- else {
- merged[counter]= arr2[countArr2];
- counter++;
- countArr2++;
- if (countArr2>=arr2.length){
- while (countArr1<arr1.length) {
- merged[counter]= arr1[countArr1];
- counter++;
- countArr1++;
- }
- }
- }
- }
- return merged;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment