Problem:
Design and implement an algorithm for three arrays.
Solution:
package array.programs;
import java.util.Scanner;
public class MergeThreeArrays {
int[] firstArray;
int[] secondArray;
int[] thirdArray;
int[] resultArray;
int[] resultArray2;
int firstArrayLimit;
int secondArrayLimit;
int thirdArrayLimit;
public static void main(String[] args) {
MergeThreeArrays mergeArrays = new MergeThreeArrays();
mergeArrays.initializeTheArray();
mergeArrays.mergeArrays(mergeArrays.firstArray, mergeArrays.secondArray, mergeArrays.firstArrayLimit, mergeArrays.secondArrayLimit, mergeArrays.resultArray);
mergeArrays.mergeArrays(mergeArrays.resultArray, mergeArrays.thirdArray, mergeArrays.resultArray.length, mergeArrays.thirdArrayLimit, mergeArrays.resultArray2);
mergeArrays.printTheArray();
}
public void initializeTheArray(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the first array elements limit");
firstArrayLimit = scanner.nextInt();
firstArray = new int[firstArrayLimit];
for (int i = 0; i < firstArray.length; i++) {
System.out.println("enter the element");
firstArray[i] = scanner.nextInt();
}
System.out.println("enter the second array elements limit");
secondArrayLimit = scanner.nextInt();
secondArray = new int[secondArrayLimit];
for (int i = 0; i < secondArray.length; i++) {
System.out.println("enter the number");
secondArray[i] = scanner.nextInt();
}
System.out.println("enter the third array elements limit");
thirdArrayLimit = scanner.nextInt();
thirdArray = new int[thirdArrayLimit];
for (int i = 0; i < thirdArray.length; i++) {
System.out.println("enter the number");
thirdArray[i] = scanner.nextInt();
}
resultArray = new int[firstArrayLimit + secondArrayLimit];
resultArray2 = new int[resultArray.length + thirdArrayLimit];
scanner.close();
}
public void mergeArrays(int[] arrayOne, int[] arrayTwo, int n1, int n2, int[] outputArray){
int i = 0;
int j = 0;
int k = 0;
while (i < n1 && j < n2 ) {
if(arrayOne[i] < arrayTwo[j]){
outputArray[k++] = arrayOne[i++];
}
else {
outputArray[k++] = arrayTwo[j++];
}
}
while(i < n1){
outputArray[k++] = arrayOne[i++];
}
while(j < n2){
outputArray[k++] = arrayTwo[j++];
}
}
public void printTheArray(){
for (int i = 0; i < resultArray2.length; i++) {
System.out.print( resultArray2[i] + " ");
}
}
}
Output:
enter the first array elements limit
2
enter the element
1
enter the element
2
enter the second array elements limit
2
enter the number
3
enter the number
4
enter the third array elements limit
2
enter the number
5
enter the number
6
1 2 3 4 5 6
Design and implement an algorithm for three arrays.
Solution:
package array.programs;
import java.util.Scanner;
public class MergeThreeArrays {
int[] firstArray;
int[] secondArray;
int[] thirdArray;
int[] resultArray;
int[] resultArray2;
int firstArrayLimit;
int secondArrayLimit;
int thirdArrayLimit;
public static void main(String[] args) {
MergeThreeArrays mergeArrays = new MergeThreeArrays();
mergeArrays.initializeTheArray();
mergeArrays.mergeArrays(mergeArrays.firstArray, mergeArrays.secondArray, mergeArrays.firstArrayLimit, mergeArrays.secondArrayLimit, mergeArrays.resultArray);
mergeArrays.mergeArrays(mergeArrays.resultArray, mergeArrays.thirdArray, mergeArrays.resultArray.length, mergeArrays.thirdArrayLimit, mergeArrays.resultArray2);
mergeArrays.printTheArray();
}
public void initializeTheArray(){
Scanner scanner = new Scanner(System.in);
System.out.println("enter the first array elements limit");
firstArrayLimit = scanner.nextInt();
firstArray = new int[firstArrayLimit];
for (int i = 0; i < firstArray.length; i++) {
System.out.println("enter the element");
firstArray[i] = scanner.nextInt();
}
System.out.println("enter the second array elements limit");
secondArrayLimit = scanner.nextInt();
secondArray = new int[secondArrayLimit];
for (int i = 0; i < secondArray.length; i++) {
System.out.println("enter the number");
secondArray[i] = scanner.nextInt();
}
System.out.println("enter the third array elements limit");
thirdArrayLimit = scanner.nextInt();
thirdArray = new int[thirdArrayLimit];
for (int i = 0; i < thirdArray.length; i++) {
System.out.println("enter the number");
thirdArray[i] = scanner.nextInt();
}
resultArray = new int[firstArrayLimit + secondArrayLimit];
resultArray2 = new int[resultArray.length + thirdArrayLimit];
scanner.close();
}
public void mergeArrays(int[] arrayOne, int[] arrayTwo, int n1, int n2, int[] outputArray){
int i = 0;
int j = 0;
int k = 0;
while (i < n1 && j < n2 ) {
if(arrayOne[i] < arrayTwo[j]){
outputArray[k++] = arrayOne[i++];
}
else {
outputArray[k++] = arrayTwo[j++];
}
}
while(i < n1){
outputArray[k++] = arrayOne[i++];
}
while(j < n2){
outputArray[k++] = arrayTwo[j++];
}
}
public void printTheArray(){
for (int i = 0; i < resultArray2.length; i++) {
System.out.print( resultArray2[i] + " ");
}
}
}
Output:
enter the first array elements limit
2
enter the element
1
enter the element
2
enter the second array elements limit
2
enter the number
3
enter the number
4
enter the third array elements limit
2
enter the number
5
enter the number
6
1 2 3 4 5 6
No comments:
Post a Comment