public Exponential(ref OutputScreen OutputWin) { Output = OutputWin; data = new Data() { time = "O(Logn)", space = "O(1)" }; Output.UpdateInfo(@"private string ExSearch(ref decimal[] input, int length, decimal target, ref int count) { //look in the first position if (input[0] == target) { return 0.ToString(); } //find the next range for the binary search int i = 1; while (i < length && input[i] <= target) { i = i * 2; } //call binary search function for found range return BSearch(ref input, i / 2, Math.Min(i, length), target, ref count); }"); }
//Algorithm public Interpolation(ref OutputScreen OutputWin) { Output = OutputWin; data = new Data() { time = "O(LogLogn)", space = "O(1)" }; Output.UpdateInfo(@"public string ISearch(decimal[] input, int min, int max, decimal key) { //midpoint container int mid; int count = 0; while (min <= max) { //get midpoint mid = (int)(min + (max - min) * ((key - input[min]) / (input[max] - input[min]))); //keep it in bounds of array if (mid < 0) { mid = 0; } else if (mid > max) { mid = max; } //if found if (key == input[mid]) { //return string return $"" Location: {mid + 1}""; } //where to look next else if (key < input[mid]) { max = mid - 1; } else { min = mid + 1; } } //return error return ""Item not found""; }"); }
public Linear(ref OutputScreen OutputWin) { Output = OutputWin; data = new Data() { time = "O(n)", space = "O(1)" }; Output.UpdateInfo(@"private string LSearch(decimal[] input, decimal target, ref int count) { //loop through the array for (int i = 0; i < input.Length; i++) { //if target then return if (input[i] == target) { return $""Location: {i + 1}""; } } //else not in the array return ""Not Found""; }"); }
public Binary(ref OutputScreen OutputWin) { Output = OutputWin; data = new Data() { time = "O(Logn)", space = "O(1)" }; Output.UpdateInfo(@"private string BSearch(decimal[] input, int start, int stop, decimal key, int length, ref int count) { //ran out of array if (start > stop) { return null; } //get midpoint int mid = (start + stop) / 2; //if found if (key == input[mid]) { //add location to string return $""Location: {mid + 1}""; } //where to look next else if (key < input[mid]) { return BSearch(input, start, mid - 1, key, length, ref count); } else { return BSearch(input, mid + 1, stop, key, length, ref count); } }"); }
public Fibonacci(ref OutputScreen OutputWin) { Output = OutputWin; data = new Data() { time = "O(Logn)", space = "O(1)" }; Output.UpdateInfo(@"private string FSearch(decimal[] input, decimal target, int length, ref int count) { //get the fib numbers int fib2 = 0; int fib1 = 1; int fib = fib2 + fib1; //get the smallest fib1/2 >= fib while (fib < length) { fib2 = fib1; fib1 = fib; fib = fib2 + fib1; } int offset = -1; while (fib > 1) { //check if fib2 is valid int i = Math.Min(offset + fib2, length - 1); //if target is greater, split array before offset if (input[i] < target) { fib = fib1; fib1 = fib2; fib2 = fib - fib1; offset = i; } //if target is greater, split array after offset else if (input[i] > target) { fib = fib2; fib1 = fib1 - fib2; fib2 = fib - fib1; } //found else { return $""Location: {i + 1}""; } } //if final element if (fib1 == 1 && input[offset + 1] == target) { return $""Location: {offset + 2}""; } //doesnt exist return ""Not Found""; }"); }