Need help with this Java project implementing an interpolation search, bolded the missing parts:
/**
A class that implements an interpolation search
and
a binary search of a sorted array of integers.
@author Frank M. Carrano
@author Timothy M. Henry
@version 4.0
*/
public class SearchComparer
{
private int[] a;
private int interpolationSearchCounter;
private int binarySearchCounter;
private static final int CAPACITY = 50;
public SearchComparer(int[] array, int n)
{
a = new int[CAPACITY];
for (int i = 0; i < n;
i++)
a[i] =
array[i];
resetCounters();
} // end default constructor
/** Resets the counters for each type of search.
*/
public final void resetCounters()
{
interpolationSearchCounter =
0;
binarySearchCounter = 0;
} // end resetCounters
/** Returns the number of searches used during an
interpolation search.
@return The number of searches used
during an interpolation search. */
public int getInterpolationSearchCount()
{
return
interpolationSearchCounter;
} // end getInterpolationSearchCount
/** Returns the number of searches used during a
binary search.
@return The number of searches used
during a binary search. */
public int getBinarySearchCount()
{
return binarySearchCounter;
} // end getBinarySearchCount
/** Searches a portion of a sorted array of
integers for a given value.
@param a
An array of integers sorted
into ascending order.
@param first
The integer index of the first array
element;
first >= 0 and < a.length
@param last
The integer index of the last
array element;
last - first >= 1; last < a.length
@param desiredItem The item
sought.
@return True if the item is found,
or false if not. */
public boolean interpolationSearch(int first, int
last, int desiredItem)
{
// ==== COMPLETE THIS METHOD
====
} // end interpolationSearch
/** Searches a portion of a sorted array of
integers for a given value.
@param a
An array of integers sorted
into ascending order.
@param first
The integer index of the first array
element;
first >= 0 and < a.length
@param last
The integer index of the last
array element;
last - first >= 1; last < a.length
@param desiredItem The item
sought.
@return True if the item is found,
or false if not. */
public boolean binarySearch(int first, int last, int
desiredItem)
{
// ==== COMPLETE THIS METHOD
====
} // end binarySearch
//
...................................................................
// Performs an interpolation search
private static void
doInterpolationSearch(SearchComparer searcher, int first,
int last, int
desiredItem, boolean willFind)
{
boolean result =
searcher.interpolationSearch(first, last, desiredItem);
System.out.print("Interpolation
search:\t");
giveMessage(desiredItem, result,
willFind);
} // end doInterpolationSearch
// Performs a binary search
private static void doBinarySearch(SearchComparer
searcher, int first, int last,
int desiredItem,
boolean willFind)
{
boolean result =
searcher.binarySearch(first, last, desiredItem);
System.out.print("Binary
search:\t\t");
giveMessage(desiredItem, result,
willFind);
} // end doBinarySearch
// Gives a message to the user as to whether the
item was found or not, and
// which outcome was expected.
private static void giveMessage(int desiredItem,
boolean result, boolean willFind)
{
if (result)
{
if
(willFind)
System.out.println("PASSES: " + desiredItem + "
was found");
else
System.out.println("FAILS: " + desiredItem + "
was not found");
} // end if
else
{
if
(willFind)
System.out.println("FAILS: " + desiredItem + "
was found");
else
System.out.println("PASSES: " + desiredItem + "
was not found");
} // end else
} // end giveMessage
// Displays the elements in the array
private static void displayArray(int[] array)
{
// ==== COMPLETE THIS METHOD
====
} // end displayArray
// Displays the counts for each type of search and
resets them.
private static void
displayAndResetCounts(SearchComparer searcher)
{
// ==== COMPLETE THIS METHOD
====
} // end displayAndResetCounts
public static void main(String[] args)
{
int a[] = {-10, -5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 20, 34, 99, 100, 200,
10000};
int b[] = {-6, -4, -2, 0, 2, 4,
6, 8, 10, 12, 14, 16, 18, 20,
22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
SearchComparer searcher = new
SearchComparer(a, a.length);
System.out.println("Searching the
array");
displayArray(a);
doInterpolationSearch(searcher,
0, a.length - 1, 14, true);
doBinarySearch(searcher, 0,
a.length - 1, 14, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -10, true);
doBinarySearch(searcher, 0,
a.length - 1, -10, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 10000, true);
doBinarySearch(searcher, 0,
a.length - 1, 10000, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 200, true);
doBinarySearch(searcher, 0,
a.length - 1, 200, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 23456, false);
doBinarySearch(searcher, 0,
a.length - 1, 23456, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -6, false);
doBinarySearch(searcher, 0,
a.length - 1, -6, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 35, false);
doBinarySearch(searcher, 0,
a.length - 1, 35, false);
displayAndResetCounts(searcher);
//
...............................
searcher = new SearchComparer(b,
b.length);
System.out.println("Searching the
uniformly distributed array");
displayArray(b);
doInterpolationSearch(searcher,
0, a.length - 1, 24, true);
doBinarySearch(searcher, 0,
a.length - 1, 24, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -6, true);
doBinarySearch(searcher, 0,
a.length - 1, -6, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 40, true);
doBinarySearch(searcher, 0,
a.length - 1, 40, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 38, true);
doBinarySearch(searcher, 0,
a.length - 1, 38, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 23456, false);
doBinarySearch(searcher, 0,
a.length - 1, 23456, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -5, false);
doBinarySearch(searcher, 0,
a.length - 1, -5, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 33, false);
doBinarySearch(searcher, 0,
a.length - 1, 33, false);
displayAndResetCounts(searcher);
} // end main
} // end SearchComparer
If you have any problem with the code feel free to comment.
Program
public class Test {
private int[] a;
private int interpolationSearchCounter;
private int binarySearchCounter;
private static final int CAPACITY = 50;
public Test(int[] array, int n) {
a = new int[CAPACITY];
for (int i = 0; i < n;
i++)
a[i] =
array[i];
resetCounters();
} // end default constructor
/** Resets the counters for each type of search.
*/
public final void resetCounters() {
interpolationSearchCounter =
0;
binarySearchCounter = 0;
} // end resetCounters
/**
* Returns the number of searches used during an
interpolation search.
*
* @return The number of searches used during an
interpolation search.
*/
public int getInterpolationSearchCount() {
return
interpolationSearchCounter;
} // end getInterpolationSearchCount
/**
* Returns the number of searches used during a binary
search.
*
* @return The number of searches used during a binary
search.
*/
public int getBinarySearchCount() {
return binarySearchCounter;
} // end getBinarySearchCount
/**
* Searches a portion of a sorted array of integers for
a given value.
*
* @param a An array of integers sorted into ascending
order.
* @param first The integer index of the first array
element; first >= 0
* and < a.length
* @param last The integer index of the last array
element; last - first
* >= 1; last < a.length
* @param desiredItem The item sought.
* @return True if the item is found, or false if
not.
*/
public boolean interpolationSearch(int first, int
last, int desiredItem) {
// ==== COMPLETE THIS METHOD
====
//loop condition
while (first <= last &&
desiredItem >= a[first] && desiredItem <= a[last])
{
interpolationSearchCounter++;//counting seraches
if (first ==
last) {//when itemisint first index
if (a[first] == desiredItem)
return true;
return false;
}
//calculating
position of the item
int pos = first
+ (((last - first) / (a[last] - a[first])) * (desiredItem -
a[first]));
if (a[pos] ==
desiredItem)//when i tem is present in the position
return true;
if (a[pos] <
desiredItem)
first = pos + 1;
else
last = pos - 1;
}
return false;
} // end interpolationSearch
/**
* Searches a portion of a sorted array of integers for
a given value.
*
* @param a An array of integers sorted into ascending
order.
* @param first The integer index of the first array
element; first >= 0
* and < a.length
* @param last The integer index of the last array
element; last - first
* >= 1; last < a.length
* @param desiredItem The item sought.
* @return True if the item is found, or false if
not.
*/
public boolean binarySearch(int first, int last, int
desiredItem) {
// ==== COMPLETE THIS METHOD
====
while(first <= last) {
binarySearchCounter++;//increasing the BinarySearchCount
int middleIndex
= (last+first)/2;//calculating middle index
//seraching for
item
if(desiredItem
< a[middleIndex])
last = middleIndex-1;
else
if(desiredItem > a[middleIndex])
first = middleIndex+1;
else
return true;
}
return false;
} // end binarySearch
// ...................................................................
// Performs an interpolation search
private static void doInterpolationSearch(Test
searcher, int first, int last, int desiredItem, boolean willFind)
{
boolean result =
searcher.interpolationSearch(first, last, desiredItem);
System.out.print("Interpolation
search:\t");
giveMessage(desiredItem, result,
willFind);
} // end doInterpolationSearch
// Performs a binary search
private static void doBinarySearch(Test searcher, int
first, int last, int desiredItem, boolean willFind) {
boolean result =
searcher.binarySearch(first, last, desiredItem);
System.out.print("Binary
search:\t\t");
giveMessage(desiredItem, result,
willFind);
} // end doBinarySearch
// Gives a message to the user as to whether the
item was found or not, and
// which outcome was expected.
private static void giveMessage(int desiredItem,
boolean result, boolean willFind) {
if (result) {
if
(willFind)
System.out.println("PASSES: " + desiredItem + "
was found");
else
System.out.println("FAILS: " + desiredItem + "
was not found");
} // end if
else {
if
(willFind)
System.out.println("FAILS: " + desiredItem + "
was found");
else
System.out.println("PASSES: " + desiredItem + "
was not found");
} // end else
} // end giveMessage
// Displays the elements in the array
private static void displayArray(int[] array) {
// ==== COMPLETE THIS METHOD
====
for(int i: array) {
System.out.print(i+" ");
}
System.out.println();
} // end displayArray
// Displays the counts for each type of search and
resets them.
private static void displayAndResetCounts(Test
searcher) {
// ==== COMPLETE THIS METHOD
====
System.out.println("Interpolation
Search Counter: "+searcher.getInterpolationSearchCount());
System.out.println("Binary Search
Counter: "+searcher.getBinarySearchCount());
searcher.resetCounters();
} // end displayAndResetCounts
public static void main(String[] args) {
int a[] = { -10, -5, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 34, 99, 100, 200, 10000
};
int b[] = { -6, -4, -2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40 };
Test searcher = new Test(a,
a.length);
System.out.println("Searching the
array");
displayArray(a);
doInterpolationSearch(searcher,
0, a.length - 1, 14, true);
doBinarySearch(searcher, 0,
a.length - 1, 14, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -10, true);
doBinarySearch(searcher, 0,
a.length - 1, -10, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 10000, true);
doBinarySearch(searcher, 0,
a.length - 1, 10000, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 200, true);
doBinarySearch(searcher, 0,
a.length - 1, 200, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 23456, false);
doBinarySearch(searcher, 0,
a.length - 1, 23456, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -6, false);
doBinarySearch(searcher, 0,
a.length - 1, -6, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 35, false);
doBinarySearch(searcher, 0,
a.length - 1, 35, false);
displayAndResetCounts(searcher);
// ...............................
searcher = new Test(b,
b.length);
System.out.println("Searching the
uniformly distributed array");
displayArray(b);
doInterpolationSearch(searcher,
0, a.length - 1, 24, true);
doBinarySearch(searcher, 0,
a.length - 1, 24, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -6, true);
doBinarySearch(searcher, 0,
a.length - 1, -6, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 40, true);
doBinarySearch(searcher, 0,
a.length - 1, 40, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 38, true);
doBinarySearch(searcher, 0,
a.length - 1, 38, true);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 23456, false);
doBinarySearch(searcher, 0,
a.length - 1, 23456, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, -5, false);
doBinarySearch(searcher, 0,
a.length - 1, -5, false);
displayAndResetCounts(searcher);
doInterpolationSearch(searcher,
0, a.length - 1, 33, false);
doBinarySearch(searcher, 0,
a.length - 1, 33, false);
displayAndResetCounts(searcher);
} // end main
} // end SearchCompare
Output
![<terminated Test (1) [Java Application] C:\Program Files\ava\jre1.8.0_211\bin\javaw.exe (22-Nov-2019, 10:56:45 am) Searching](http://img.homeworklib.com/questions/7d813b70-8865-11ec-ac77-59e5c32cfe99.png?x-oss-process=image/resize,w_560)
Need help with this Java project implementing an interpolation search, bolded the missing parts: /** ...
I am currently using eclipse to write in java.
A snapshot of the output would be greatly appreciated to verify
that the program is indeed working. Thanks in advance for both your
time and effort.
Here is the previous exercise code:
/////////////////////////////////////////////////////Main
/*******************************************
* Week 5 lab - exercise 1 and exercise 2: *
* ArrayList class with search algorithms *
********************************************/
import java.util.*;
/**
* Class to test sequential search, sorted search, and binary search
algorithms
* implemented in...
JAVA Algorithms course need help completing this method using binary search thank you in advance. ------------------------------------------------------------------------------------------------- /** A class for executing binary searches in an array. */ public class BinarySearcher { /** Finds a value in a range of a sorted array, using the binary search algorithm. @param a the array in which to search @param low the low index of the range @param high the high index of the range @param value the value to find @return the index...
I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...
Java Quadratic Probing Hash table HELP! Complete the Map and Entry class provided in Map.java. Create a tester/driver class to show the Map class is working as intended. Methods/Constructor correctly completed (2pt) This is the Map class methods Map(), put(key, value), get(key), isEmpty(), makeEmpty() Private class correctly completed (2pt) This is the Entry class methods Add the methods that are required for this to work correctly when stored in the QuadraticProbingHashTable Tester class (1pt) Show the methods of the Map...
#2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26 #Trace the sorted data set using a binary search. Search target: 23 #Trace the sorted data set using a binary search. Search target: 15 (To trace a binary search, list the value of first, last, and mid for each pass through the method.) Use this method: private static <T extends Comparable<? super T>> boolean...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...
Need help with this Java. I need help with the "to do" sections.
Theres two parts to this and I added the photos with the entire
question
Lab14 Part 1:
1) change the XXX to a number in the list, and YYY to a
number
// not in the list
2.code a call to linearSearch with the item number
(XXX)
// that is in the list; store the return in the variable
result
3. change both XXX numbers to the...