Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add merge() method to the OrdArray class in the orderedArray.java program so tha

ID: 3665123 • Letter: A

Question

Add merge() method to the OrdArray class in the orderedArray.java program so that you can merge two ordered sources arrays into an ordered destination array. Write code in main() that inserts some random numbers into two source arraysm invokes merge(), and displays the contents of the resulting destination array. The source arrays may hold different numbers of data items. In the algorithm you will need to compare the keys of the source arrays, picking the smallest one to copy to the destination. You'll also need to handle the situation when one sourse array exhausts ints contents before the other.

The code for orderedArray.java

class OrdArray
{
   private long[] a;
   private int nElems;
  
   public OrdArray(int max)
   {
       a = new long[max];
       nElems = 0;
   }
  
   public int size()
   {
       return nElems;
   }
  
   public int find(long searchKey)
   {
       int lowerBound = 0;
       int upperBound = nElems - 1;
       int curIn;
      
       while(true)
       {
           curIn = (lowerBound + upperBound) / 2;
           if(a[curIn] == searchKey)
               return curIn;
           else if(lowerBound > upperBound)
               return nElems;
           else
           {
               if(a[curIn] < searchKey)
                   lowerBound = curIn + 1;
               else
                   upperBound = curIn - 1;
              
           }
       }
   }
}

Explanation / Answer

public class OrdArray
{
private long[] a; // for array of a
private int nNumbers; // required numbers

public OrdArray(int max) // declared constructor
{
a = new long[max]; // declared array
nNumbers = 0;
}

public int size()
{
return nNumbers;
}

public int find(long searchKey)// declaration for searching
{
int lowerBound = 0;
int upperBound = nNumbers-1;
int curIn;

while (true)
{
curIn = (lowerBound + upperBound ) / 2;
if (a[curIn] == searchKey)
return curIn; // searching is fonud continue the process
else if (lowerBound > upperBound)
return nNumbers;
else
{
if (a[curIn] < searchKey)
lowerBound = curIn + 1; // devide two parts upper and lower
else
upperBound = curIn - 1; //
}   
}   
}

public void insert(long value) // insert element into array
{
int n;
for (n = 0; n < nNumbers; n++) // serach the location
if (a[n] > value)
break;
for (int p = nNumbers; p > n; p--) // move largest element
a[k] = a[k-1];
a[n] = value; // place it
nNumbers++; // increse the array
}   

public boolean delete(long value)//element not found
{
int n = find(value);
if (n == nNumbers)
return false;
else
{
for (int p = n; p < nNumbers; p++) // move largest element
a[p] = a[p+1];
nNumbers--; // decrese the array size
return true;
}
}   

public void display() // to daiplay
{
for (int n = 0; n < nNumbers; n++)
System.out.print(a[j] + " ");   
System.out.println("");
}

public static long[] merge(OrdArray a, OrdArray b)
{

long[] c = new long[a.nNumbers + b.nNumbers];
int m = 0, n = 0, p = 0;

while (m < a.nNumbers && n < b.nNumbers)
{
if (a.data[m] < b.data[n])   
c[p++] = a.data[m++];
else
c[p++] = b.data[n++];
}

while (m < a.nNumbers)
c[p++] = a.data[m++];

while (n < b.nNumbers)
c[p++] = b.data[n++];
return c;
}
} // end class

class OrderedApp
{
public static void main(String[] args)
{
int maxSize = 100; // declared the size of the array
OrdArray a, b, c; // array manes
a = new OrdArray(maxSize); // create array
b = new OrdArray(maxSize);
c = new OrdArray(maxSize);

a.insert(9);
a.insert(11);
a.insert(16);
a.insert(19);
a.insert(20);
a.insert(29);
a.insert(32);
a.insert(36);
a.insert(38);
a.insert(40);

b.insert(13);
b.insert(18);
b.insert(21);
b.insert(24);
b.insert(26);
b.insert(28);
b.insert(35);
b.insert(39);
b.insert(42);
b.insert(45);

OrdArray.merge(a,b);

System.out.print("Array a: ");
a.display();
System.out.println();
System.out.print("Array b: ");
b.display();
System.out.println();
System.out.print("Array c: ");
c.display();
System.out.println();
}   
}


output:9,11,13,16,18,19,20,21,24,26,28,29,32,35,36,38,39,42,45

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote