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.
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; } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
