You are given two sorted arrays, A and B and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.
File:ArrayOperations.java
----------------------------------------------------------
package org.developersbrain.solutions;
import java.util.Arrays;
public class ArrayOperations {
static int returnEmptyElements(int[] arrA){
int zeroElement=0,nonZeroElement = 0;
for(int i=0;i<arrA.length;i++){
if(arrA[i]==0){
zeroElement=i;
}else{
nonZeroElement=i;
}
}
return nonZeroElement+1;
}
//Straightforward method - Append the array in Array 1
public static int[] mergeArray(int[] arrA,int[] arrB){
int i=returnEmptyElements(arrA);
int[] arrCopy=new int[(i+arrB.length)];
if((i+arrB.length)>arrA.length){
System.out.println("Array cannot be merged due to size limit in Array 1.");
return arrA;
}else{
int j;
for(j=0;j<i;j++){
arrCopy[j]=arrA[j];
}
for(int k=0;k<arrB.length;k++,j++){
arrCopy[j] = arrB[k];
}
}
return arrCopy;
}
public static void main(String[] args){
int[] a=new int[25];
int[] b=new int[10];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
a[5]=6;
a[6]=7;
a[7]=8;
a[8]=9;
a[9]=10;
b[0]=6;
b[1]=7;
b[2]=8;
b[3]=9;
b[4]=10;
b[5]=11;
b[6]=12;
b[7]=13;
b[8]=14;
b[9]=15;
System.out.println("Array 1");
for(int aV:a) System.out.print(aV+ " ");
System.out.println();
System.out.println("Array 2");
for(int bV:b) System.out.print(bV+ " ");
System.out.println();
a=mergeArray(a,b);
Arrays.sort(a);//You can implement your own sorting technique
System.out.println("Merged Array");
for(int aV:a) System.out.print(aV+ " ");
}
}
----------------------------------------------------------
package org.developersbrain.solutions;
import java.util.Arrays;
public class ArrayOperations {
static int returnEmptyElements(int[] arrA){
int zeroElement=0,nonZeroElement = 0;
for(int i=0;i<arrA.length;i++){
if(arrA[i]==0){
zeroElement=i;
}else{
nonZeroElement=i;
}
}
return nonZeroElement+1;
}
//Straightforward method - Append the array in Array 1
public static int[] mergeArray(int[] arrA,int[] arrB){
int i=returnEmptyElements(arrA);
int[] arrCopy=new int[(i+arrB.length)];
if((i+arrB.length)>arrA.length){
System.out.println("Array cannot be merged due to size limit in Array 1.");
return arrA;
}else{
int j;
for(j=0;j<i;j++){
arrCopy[j]=arrA[j];
}
for(int k=0;k<arrB.length;k++,j++){
arrCopy[j] = arrB[k];
}
}
return arrCopy;
}
public static void main(String[] args){
int[] a=new int[25];
int[] b=new int[10];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
a[5]=6;
a[6]=7;
a[7]=8;
a[8]=9;
a[9]=10;
b[0]=6;
b[1]=7;
b[2]=8;
b[3]=9;
b[4]=10;
b[5]=11;
b[6]=12;
b[7]=13;
b[8]=14;
b[9]=15;
System.out.println("Array 1");
for(int aV:a) System.out.print(aV+ " ");
System.out.println();
System.out.println("Array 2");
for(int bV:b) System.out.print(bV+ " ");
System.out.println();
a=mergeArray(a,b);
Arrays.sort(a);//You can implement your own sorting technique
System.out.println("Merged Array");
for(int aV:a) System.out.print(aV+ " ");
}
}
Output:
--------------------------------------------------
Array 1
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Array 2
6 7 8 9 10 11 12 13 14 15
Merged Array
1 2 3 4 5 6 6 7 7 8 8 9 9 10 10 11 12 13 14 15
Comments
Post a Comment