Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

binary search between two indexes

 int BinarySearch(int[] arr, int low, int high, int key)
 {
     while (low <= high)
     {
         int mid = low + (high - low) / 2;
         int midVal = arr[mid];

        if (midVal < key) low = mid + 1;
        else if (midVal > key) high = mid - 1;
        else if (midVal == key) return mid;
    }
    return -1;
}
 
PREVIOUS NEXT
Tagged: #binary #search #indexes
ADD COMMENT
Topic
Name
1+7 =