105 lines
3.4 KiB
Java
105 lines
3.4 KiB
Java
package Assignments.A3;
|
|
|
|
public class InterpolationSearch {
|
|
|
|
/** Holds if the value was found or not. */
|
|
private final boolean found;
|
|
/** Holds the index of the key, -1 if not found. */
|
|
private final int index;
|
|
/** Holds the number of divisions used to perform the search. */
|
|
private int divisions;
|
|
|
|
/**
|
|
* Accepts the values, performs the search, and assigns the fields to the corresponding output data.
|
|
* <p>
|
|
* This constructor accepts in the array to be searched and the key to be searched for. Based off of this,
|
|
* the method will perform some precondition checks to ensure the values are valid and then perform the
|
|
* search using the formula provided in the slide deck.
|
|
* </p>
|
|
*
|
|
* @param array The list of values being search through.
|
|
* @param key The desired value being searched for.
|
|
*
|
|
* @throws IllegalArgumentException If the array is empty.
|
|
*/
|
|
public InterpolationSearch(int[] array, int key) {
|
|
/* Precondition for empty list. */
|
|
if (array.length == 0) {
|
|
throw new IllegalArgumentException("The searched array cannot be empty.");
|
|
}
|
|
|
|
/* Precondition check to see if the list is size 1 element. */
|
|
if (array.length == 1) {
|
|
this.found = (array[0] == key);
|
|
this.divisions = 0;
|
|
this.index = (array[0] == key ? 0 : -1);
|
|
return;
|
|
}
|
|
|
|
/* Sets the default values for the search */
|
|
int low = 0;
|
|
int high = array.length-1;
|
|
this.divisions = 0;
|
|
|
|
/* Iterates as long as high is not less than low */
|
|
while (high >= low) {
|
|
int probe;
|
|
|
|
/* Checks if high is the same thing as low to prevent division by zero. */
|
|
if (high != low) {
|
|
probe = low + (((key - array[low]) * (high - low)) / (array[high] - array[low]));
|
|
|
|
/* This is here due to the fact that it is not always divided as there is only 1 element left. */
|
|
this.divisions++;
|
|
} else {
|
|
probe = high;
|
|
if (array[probe] != key) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Checks if it passed the element due to it not being in list. */
|
|
if (array[high] < key || array[low] > key) {
|
|
break;
|
|
}
|
|
/* Checks if the key is less than the probe's value */
|
|
if (array[probe] > key) {
|
|
high = probe - 1;
|
|
} else if (array[probe] < key) { /* Checks if the key is greater than the probes value */
|
|
low = probe + 1;
|
|
} else { /* The key was found at the probes index. */
|
|
this.found = true;
|
|
this.index = probe;
|
|
return;
|
|
}
|
|
}
|
|
/* Sets the values if the key was not in the list. */
|
|
this.found = false;
|
|
this.index = -1;
|
|
}
|
|
|
|
/**
|
|
* Gets if the key was found.
|
|
* @return if it was found.
|
|
*/
|
|
public boolean isFound() {
|
|
return found;
|
|
}
|
|
|
|
/**
|
|
* Gets the number of divisions it took for the search to conclude.
|
|
* @return the number of divisions
|
|
*/
|
|
public int getDivisions() {
|
|
return divisions;
|
|
}
|
|
|
|
/**
|
|
* Gets the index at which the key was found. If the key was not found it returns -1.
|
|
* @return the found index.
|
|
*/
|
|
public int getIndex() {
|
|
return index;
|
|
}
|
|
}
|