/* Lab : Lab9 Day : Tuesday [ 21 Oct 08 ] Problem Number: 02 Problem Title : Merging Two Sorted Arrays Theory : During merging the smallest element one of the two smallest items of array A and B is inserted into the merged array by deleting it from its place. there can be one of following three cases 1. both A and B has elements the smallest element among two array is deleted and inserted in merged array. 2. Only A has elements delete all elements of A one by one and insert to merged array at end. 3. Only B has elements delete all elements of B one by one and insert to merged array at end. */ import java.util.Random; class sol_lab9_prob02{ // function to sort an array public static void bubbleSort(int []A) { int i, j; for( i = 0 ; i < A.length ; i = i + 1 ) for( j = i ; j < A.length ; j = j + 1 ) if ( A[i] > A[j] ) { int temp; temp = A[i]; A[i] = A[j]; A[j] = temp; } } // function to merge array A and B public static int[] merge(int []A, int []B) { int []mergedAB = new int[ A.length + B.length ]; int i_A, i_B, i ; i_A = 0; i_B = 0; for( i = 0 ; i < A.length + B.length ; i = i + 1 ) { // If both array A and B has items if ( ( i_A < A.length ) && ( i_B < B.length ) ) { if ( A[i_A] < B[i_B] ) { mergedAB[i] = A[i_A]; i_A = i_A + 1; // logically deleting an item from A } else { mergedAB[i] = B[i_B]; i_B = i_B + 1; // logically deleting an item from B } } else { // If only array A has items if ( i_A < A.length ) { mergedAB[i] = A[i_A]; i_A = i_A + 1; // logically deleting an item from A } // If only array B has items else { mergedAB[i] = B[i_B]; i_B = i_B + 1; // logically deleting an item from B } } } return mergedAB; } public static void main(String args[]) { int lengthOfArrayA, lengthOfArrayB, i ; lengthOfArrayA = Integer.parseInt(args[0]); lengthOfArrayB = Integer.parseInt(args[1]); int []A = new int[lengthOfArrayA] ; int []B = new int[lengthOfArrayB] ; // Generating random numbers to fill in array A Random rand = new Random(); for( i = 0 ; i