Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with this C# question. The following code implements a binary search throug

ID: 664106 • Letter: H

Question

 Help with this C# question. The following code implements a binary search through an array of integers.   Rewrite this function to use the more generic T and "array of T'' data type in place of int and array of integers:  public int BinarySearch(int searchElement, int [] data) {    int low = 0;    int high = data.Length - 1;    int middle = (low + high + 1) / 2;    int location = -1;    do     {      if ( searchElement == data[middle] )         location = middle;      else if ( searchElement < data[middle] )         high = middle - 1;      else          low = middle + 1;      middle = (low + high + 1) / 2;    } while (( low <= high ) && (location == -1));    return location } 

Explanation / Answer

public static int BinarySearch(T[] array, T searchFor, Comparer comparer) { int high, low, mid; high = array.Length - 1; low = 0; if (array[0].Equals(searchFor)) return 0; else if (array[high].Equals(searchFor)) return high; else { while (low 0) high = mid - 1; else low = mid + 1; } return -1; } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote