I'm using the following c# binary search algorithm to search for doubles in a double array containing 256 - 4096 elements:
- public static void Search(double[] array, double key)
- {
- Quicksort.Sort(array);
- int min = 0;
- int max = array.Length - 1;
- bool found = false;
-
- while (min <= max)
- {
- int mid = (min + max) / 2;
-
- if (array[mid] == key)
- {
- found = true;
- Console.WriteLine($"{key} was found at index {mid} of the array");
- }
- else if (array[mid] > key)
- max = mid - 1;
- else if (array[mid] < key)
- min = mid + 1;
- }
- if (found == false)
- Console.WriteLine($"{key} wasn't found in the array.");
When i try to run it, i get a stack overflow exception thrown... How do i fix it?
Thanks for the help!