class OrdArray
{
private double[] a; // ref to array a
private int nElems; // number of data items
//----------------------------------------------------------
public OrdArray(int max) // constructor
{
a = new double[max]; // create array
nElems = 0;
}
//----------------------------------------------------------
public int size()
{ return nElems; }
//----------------------------------------------------------
public int find(double searchKey)
{
int lowerBound = 0;// arrayA is empty,
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it else
if(lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
//----------------------------------------------------------
public void insert(double value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move higher ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
//----------------------------------------------------------
public boolean delete(double value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
//----------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//----------------------------------------------------------
} // end class OrdArray
class OrderedApp
{
public static void main (String[] args)
{
int maxsize = 100;
OrdArray arr, arr1, arr2;
arr = new OrdArray(maxsize);
arr1 = new OrdArray(maxsize);
arr2 = new OrdArray(maxsize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr1.insert(88);
arr1.insert(11);
arr1.insert(00);
arr1.insert(66);
arr1.insert(33);
arr2.merge( arr1 , arr);
}
public void merge(int[] arr, int sizeArr,
int[] arr1, int sizearr1,
int[] arr2)
{
int aDex=0, bDex=0, arr2x=0;
while(aDex < sizeArr && bDex < sizearr1)
if( arr[aDex] < arr1[bDex] )
arr2[arr2x++] = arr[aDex++];
else
arr2[arr2x++] = arr1[bDex++];
while(aDex < sizeArr)
arr2[arr2x++] = arr[aDex++];
while(bDex < sizearr1)
arr2[arr2x++] = arr1[bDex++];
}
}
The main method cannot be changed. like i am not allowed to. so the call has to be arr2.merge(arr , arr1)
that being said i know my merge method works
i just need to call it
or if my merge method is wrong can somebody help me write a merge method to merge arr1 and arr into arr2 without changing anything the main method
i just need a merge method to be called with those parameters
the book the code is taken form is called java data structures and algorithms
if you need more info please do say so