Hi I really need some help on this java code. Please see code and then instructi
ID: 3563169 • Letter: H
Question
Hi I really need some help on this java code. Please see code and then instructions that follow. Thanks!
public int binarySearch(Comparable[] objArray, Comparable searchObj)
{
int low = 0;
int high = objArray.length - 1;
int mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (objArray[mid].compareTo(searchObj) < 0)
{
low = mid + 1;
}
else if (objArray[mid].compareTo(searchObj) > 0)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
Modify the binary search algorithm such that the following conditions
are met:
1. The algorithm returns an index where a specified item should be inserted such that the
ordering of all items will be preserved after the insertion has occurred.
2. The algorithm should always return a non-negative integer regardless of whether or not
the item to be inserted is already in the array.
3. Algorithm must use Comparable objects. Do not write the algorithm so that it only
works with primitive data values such as int or double you can create an array of Integer objects by typing:Integer[] intArray = {1, 2, 3, 4, 5, 6};.
work.
Testing The Implementation
There are 3 conditions that need to be tested that are related to the positioning of the item being inserted:
1. the lower boundary of the array
2. the upper boundary of the array
3. between the upper and lower boundaries of the array
For each of the three conditions there are two possibilities:
1. the item already exists in the array
2. the item does not exist in the array
Furthermore, the array may contain:
1. an odd number of items, or
2. an even number of items.
The combinations of the above factors yield a total of 3 * 2 * 2, or 12 test categories, assuming the arrayis not empty. The empty array yields a 13th test category, which should also be tested.
To test your code, do the following:
1. Create a set of 13 test cases, one test case for each of the 13 test categories described
above. Recall that every test case should consist of 1) inputs and 2) expected output. Each
test case should consist of an array of Comparable objects and an object of the same type
to be inserted into the array, as inputs, and the correct insertion point for the item to be
inserted, as the expected output. You can create your tests using jUnit or some other
testing library, or you can create your own test harness. A test harness is simply a program
that passes the inputs of test cases to the code to be tested, and checks the actual output
against the expected output. The results are displayed on the screen or written to a testing
log file.
2. With test cases, test modified binarySearch method
Need a table listing the following information: each of the 13 test categories
a. each of the 13 test categories
b. the array of itemsused for each test
c. the item tested for insertion for each test
d. the index expected the algorithm to return
e. the index the algorithm actually returned
Explanation / Answer
#include int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements "); scanf("%d",&n); printf("Enter %d integers ", n); for ( c = 0 ; cRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.