ISC Practicals and Theory - Array Combiner
Updated 21 Oct 2026
Array combining means joining two arrays into a third array that holds every element of both, here arranged in ascending order. This version uses a class Combiner with a default constructor; the method combine() receives the two arrays as objects passed as parameters and fills the current object.
Definition
If the first array is $\{5, 1, 9\}$ and the second is $\{8, 1, 3, 5\}$, the combined array has $3 + 4 = 7$ elements. Duplicates are kept, so the result in ascending order is $\{1, 1, 3, 5, 5, 8, 9\}$.
The sorting is done by hand with bubble sort: in each pass, neighbouring elements are compared and swapped if the left one is bigger, so the largest remaining element moves to the end. No library sorting method is used.
Algorithm
maincreates three objects ofCombiner:x,yandz. The default constructor setssizeto 0 andarrto an empty array.maincallsx.accept(sc)and theny.accept(sc):accept()readssize; if it is less than 1 or more than 20 it returnsfalse, otherwise it createsarr, reads the elements with a loop and returnstrue.- Because of
&&, the second array is read only if the first was valid.
- If either call returned
false,mainprintsINVALID SIZEand stops. - Otherwise
maincallsz.combine(x, y):sizebecomesa.size + b.sizeandarris created with that size;- a loop copies the elements of
a, then another loop copies the elements ofb, using the position counterk; sort()is called.
sort()runs bubble sort: for each passi, the inner loop comparesarr[j]witharr[j + 1]forjfrom 0 tosize - 2 - iand swaps them with the help oftwhenarr[j] > arr[j + 1].maincallsdisplay()forx,yandzto print the two inputs and the combined sorted array.
Program
The data members arr and size belong to each object; accept() reads one array, combine() joins two, sort() orders the result and display() prints it.
import java.util.Scanner;
/* Class Combiner stores an integer array and its size.
combine() merges the elements of two Combiner objects into the current object
and sorts them in ascending order with a hand-written sorting method. */
public class Combiner
{
int arr[]; // data member: stores the elements of the array
int size; // data member: number of elements stored in arr
/* Default constructor: starts with an empty array. */
Combiner()
{
size = 0;
arr = new int[0];
}
/* Reads the size and the elements from the keyboard.
sc : the Scanner used for input (one Scanner is shared by all objects)
returns true if the size is valid (1 to 20), otherwise false */
boolean accept(Scanner sc)
{
System.out.print("Enter the size of the array (1 to 20): ");
size = sc.nextInt();
if (size < 1 || size > 20) // size outside the allowed range
{
return false;
}
arr = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < size; i++) // read the elements one by one
{
arr[i] = sc.nextInt();
}
return true;
}
/* Fills the current object with all the elements of a and b (duplicates are kept)
and then sorts them in ascending order.
a, b : the two Combiner objects passed as parameters
returns nothing */
void combine(Combiner a, Combiner b)
{
size = a.size + b.size; // the new array holds every element of both
arr = new int[size];
int k = 0; // position in the combined array
for (int i = 0; i < a.size; i++)
{
arr[k++] = a.arr[i]; // copy the elements of a first
}
for (int i = 0; i < b.size; i++)
{
arr[k++] = b.arr[i]; // then the elements of b
}
sort();
}
/* Sorts arr in ascending order by bubble sort. returns nothing */
void sort()
{
for (int i = 0; i < size - 1; i++) // number of passes
{
for (int j = 0; j < size - 1 - i; j++) // compare neighbouring elements
{
if (arr[j] > arr[j + 1]) // out of order: swap them
{
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
/* Prints the elements of the array on one line, separated by spaces. returns nothing */
void display()
{
for (int i = 0; i < size; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // one Scanner for both arrays
Combiner x = new Combiner(); // first array
Combiner y = new Combiner(); // second array
Combiner z = new Combiner(); // combined sorted array
if (x.accept(sc) && y.accept(sc)) // second array is read only if the first was valid
{
z.combine(x, y);
System.out.println("First array:");
x.display();
System.out.println("Second array:");
y.display();
System.out.println("Combined sorted array:");
z.display();
}
else
{
System.out.println("INVALID SIZE");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
arr | int[] | Data member that stores the elements of the array |
size | int | Data member for the number of elements in arr |
sc | Scanner | Reads the sizes and elements typed by the user; created in main and passed to accept() |
a | Combiner | Parameter of combine(): the first array object |
b | Combiner | Parameter of combine(): the second array object |
k | int | Position in the combined array where the next element is copied |
i | int | Loop counter for reading, copying and for the pass number in sort() and the loops of display() |
j | int | Inner loop counter in sort(): position of the element being compared |
t | int | Temporary variable used to swap arr[j] and arr[j + 1] |
x | Combiner | Object in main holding the first array |
y | Combiner | Object in main holding the second array |
z | Combiner | Object in main that receives the combined sorted array |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter the size of the array (1 to 20): 3
Enter 3 elements:
5 1 9
Enter the size of the array (1 to 20): 4
Enter 4 elements:
8 1 3 5
First array:
5 1 9
Second array:
8 1 3 5
Combined sorted array:
1 1 3 5 5 8 9 Sample 2
Enter the size of the array (1 to 20): 1
Enter 1 elements:
7
Enter the size of the array (1 to 20): 1
Enter 1 elements:
7
First array:
7
Second array:
7
Combined sorted array:
7 7 Sample 3
Enter the size of the array (1 to 20): 2
Enter 2 elements:
4 4
Enter the size of the array (1 to 20): 3
Enter 3 elements:
4 2 4
First array:
4 4
Second array:
4 2 4
Combined sorted array:
2 4 4 4 4 Sample 4
Enter the size of the array (1 to 20): 21
INVALID SIZESample 5
Enter the size of the array (1 to 20): 3
Enter 3 elements:
1 2 3
Enter the size of the array (1 to 20): 0
INVALID SIZEDry run
Take the first sample: $x = \{5, 1, 9\}$ and $y = \{8, 1, 3, 5\}$. combine() copies the elements, giving arr = 5 1 9 8 1 3 5 with size = 7. Then sort() runs bubble sort. The array after each pass is:
| Pass (i) | Swaps made | arr after the pass |
|---|---|---|
| start | - | 5 1 9 8 1 3 5 |
| 0 | 5 and 1, 9 and 8, 9 and 1, 9 and 3, 9 and 5 | 1 5 8 1 3 5 9 |
| 1 | 8 and 1, 8 and 3, 8 and 5 | 1 5 1 3 5 8 9 |
| 2 | 5 and 1, 5 and 3 | 1 1 3 5 5 8 9 |
| 3 | none | 1 1 3 5 5 8 9 |
| 4 to 5 | none | 1 1 3 5 5 8 9 |
display() then prints 1 1 3 5 5 8 9, as in the first sample run.
Variations
- Merge sorted arrays "Two arrays are already sorted in ascending order. Combine them into a third sorted array without sorting again."
- Without duplicates "Combine the two arrays and print the sorted result with repeated elements removed."
- Descending order "Combine the two arrays and arrange the result in descending order."