In: Computer Science
I get errors in my merge method and Im not sure how to fix it to make the code work
public class OrderedApp1
{
public static void main(String[] args)
{
int maxSize = 100; // array size
int searchKey = 55;
OrdArray1 arr, a1, a2, a3; // reference to arrays
arr = new OrdArray1(maxSize); // create the arrays
a1 = new OrdArray1(maxSize);
a2 = new OrdArray1(maxSize);
a3 = new OrdArray1(maxSize);
//int a3[] =new int [ a1.length + a2.length];
a1.insert(3); a1.insert(16); a1.insert(23); a1.insert(44);a1.insert(55);
a2.insert(11); a2.insert(77);a2.insert(100); //insert 10 items
arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22);
arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);
// search for item
System.out.println("Array a1 is");
a1.display();
System.out.println("Array a2 is");
a2.display();
System.out.println("Array a3 is");
for (int i =0 ; i < a1.length + a2.length;i++)
a3.merge(a1, a2, a3);
a3.display();
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display();
arr.delete(00); arr.delete(55); arr.delete(99); //delete 3 items
System.out.print("After deletion, ");
arr.display();
}
}
public class OrdArray1
{
private long[] a; // ref to array a
private int nElems; // number of data items
public int length;
//************************************************************************
public OrdArray1(int max) // constructor
{
a = new long[max]; // create array
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; // 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
}
}
}
//*************************************************************************
public void insert(long value) // put element into array
{
int j;
for(j=0; j
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
}
//*************************************************************************
public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
}
//**************************************************************************
public void display() // displays array contents
{
for(int j=0; j
System.out.print(a[j] + " ");
System.out.println(" ");
}
public void merge (int a1, int a2, int a3)
{
int i=0, j=0, k=0;
while ( i
{
if (a1[i] < a2[j])
a3[k+1] = a1[i++];
else
a3[k++] = a2[j++];
}
while (i < a1.length)
a3[k++] = a1[i++];
while (j< a2.length)
a3[k++] =a2[j++];
}
}
// Below code is working fine ,the main fault in your code was ,in merge method you were merging //OrdArray1 class's object,but we want to merge array of OrdArray1 class's object.
/*
i did it in the following way in merge method.
where a1 is array of first argument object passed in function.
where a2 is array of second argument object passed in function.
where a3 is array of third argument object passed in function.
long a1[]=d.a;
long a2[]=b.a;
long a3[]=c.a;
*/
// save the following code in OrderedApp1.java.
public class OrderedApp1
{
public static void main(String[] args)
{
int maxSize = 100; // array size
int searchKey = 55;
OrdArray1 arr, a1, a2, a3; // reference to arrays
arr = new OrdArray1(maxSize); // create the arrays
a1 = new OrdArray1(maxSize);
a2 = new OrdArray1(maxSize);
a3 = new OrdArray1(maxSize);
//int a3[] =new int [ a1.length + a2.length];
a1.insert(3); a1.insert(16); a1.insert(23); a1.insert(44);a1.insert(55);
a2.insert(11); a2.insert(77);a2.insert(100); a2.insert(12); a2.insert(34); //insert 10 items
arr.insert(77); arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22);
arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);
// search for item
System.out.println("Array a1 is");
a1.display();
System.out.println("Array a2 is");
a2.display();
System.out.println("Array a3 is");
a3.display();
for (int i =0 ; i < a1.length + a2.length;i++)
a3.merge(a1, a2, a3);
a3.display();
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display();
arr.delete(00); arr.delete(55); arr.delete(99); //delete 3 items
System.out.print("After deletion, ");
arr.display();
}
}
class OrdArray1
{
private long[] a; // ref to array a
private int nElems; // number of data items
public int length;
//************************************************************************
public OrdArray1(int max) // constructor
{
a = new long[max]; // create array
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; // 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
}
}
}
//*************************************************************************
public void insert(long value) // put element into array
{
int j;
for(j=0; j<nElems;j++)
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
}
//*************************************************************************
public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems-1;k++)
a[k]=a[k+1];
nElems--; // decrement size
return true;
}
}
//**************************************************************************
public void display() // displays array contents
{
for(int j=0; j<nElems;j++)
System.out.print(a[j] + " ");
System.out.println(" ");
}
public void merge (OrdArray1 d, OrdArray1 b, OrdArray1 c)
{
long a1[]=d.a;
long a2[]=b.a;
long a3[]=c.a;
int i=0, j=0, k=0;
while ( i<nElems && j<nElems )
{
if (a1[i] < (a2[j]))
a3[k++] = a1[i++];
else
a3[k++] = a2[j++];
}
while (i < a1.length)
a3[k++] = a1[i++];
while (j< a2.length)
a3[k++] =a2[j++];
}
}