//Contains the interpolation search algorithm public static void InterpolationSearch(float[] array, float toFind, int low, int high) { int boundaryCode = 0; int mid = 0; while (low <= high && toFind >= array[low] && toFind <= array[high]) { //The midpoint of the array is found by estimating where the value being searched for should lie within the array, assuming equal distribution mid = (int)Math.Round(low + (((double)(high - low) / (array[high] - array[low])) * (toFind - array[low]))); //If the value at the midpoint is equal to the value being searched for, tell the user and find other instances of the value if (array[mid] == toFind) { Console.WriteLine("Value {0} found at index {1}", toFind, mid); GenericSearchingAlgorithms.FindOtherInstances(array, toFind, mid); } //If the value at the midpoint is less than the value being searched for, change the low index so that the left side of the array is effectively discarded if (array[mid] < toFind) { low = mid + 1; } //Otherwise, discard the right side of the array else { high = mid - 1; } iterations++; } //If the value is not found in the array at all, tell the user and get the closest value in the array if (array[mid] != toFind) { Console.WriteLine("Value {0} not found in dataset. ", toFind); boundaryCode = -1; GenericSearchingAlgorithms.GetClosestValue(array, mid, toFind, boundaryCode); } else if (toFind > array[array.Length - 1]) { boundaryCode = 1; GenericSearchingAlgorithms.GetClosestValue(array, mid, toFind, boundaryCode); } else if (toFind < array[0]) { boundaryCode = 0; GenericSearchingAlgorithms.GetClosestValue(array, mid, toFind, boundaryCode); } }
//Contains the binary search algorithm public static void BinarySearch(float[] array, float toFind, int left, int right) { int boundaryCode = 0; //If the value is not out of range of the array, but it is not found in the array, find the closest value using boundary code -1 if (left > right) { boundaryCode = -1; GenericSearchingAlgorithms.GetClosestValue(array, left - 1, toFind, boundaryCode); return; } //If the value being searched for is less than the leftmost value in the array, find the closest value using boundary code 0 if (toFind < left) { boundaryCode = 0; GenericSearchingAlgorithms.GetClosestValue(array, left, toFind, boundaryCode); return; } //If the value being searched for is greater than the rightmost value in the array, find the closest value using boundary code 1 else if (toFind > right) { boundaryCode = 1; GenericSearchingAlgorithms.GetClosestValue(array, right, toFind, boundaryCode); return; } //The midpoint between the current left and right indexes are found (initially set to 0, and the length of the array - 1) int mid = (int)Math.Floor((double)right + left / 2); //If the value is found at the midpoint, tell the user and find any other instances if (toFind == array[mid]) { Console.WriteLine("\nValue {0} found at index {1}. ", toFind, mid); GenericSearchingAlgorithms.FindOtherInstances(array, toFind, mid); } //If the value being searched for is less than the midpoint, 'discard' the right side of the array and perform a binary search on the left side else if (toFind < array[mid]) { BinarySearch(array, toFind, left, mid - 1); } //If the value being searched for is greater than the midpoint, 'discard' the left side of the array and perform a binary search on the right side else { BinarySearch(array, toFind, mid + 1, right); } iterations++; }