binary search: remove superfluous variable

This commit is contained in:
Roland W-H 2022-05-26 16:51:14 +01:00
parent 38deb15d4d
commit 063a229f3a
1 changed files with 5 additions and 6 deletions

View File

@ -14,16 +14,15 @@
int bsrch(int target, int* arr, size_t n) int bsrch(int target, int* arr, size_t n)
{ {
// TODO: Find a way to error if list is out of order // TODO: Find a way to error if list is out of order
bool found = false; int start = 0, end = (int)n - 1;
int start = 0, end = n - 1;
if (arr[start] > target || arr[end] < target) return -1; if (arr[start] > target || arr[end] < target) return -1;
int mid = (start + end) / 2; int mid = (start + end) / 2;
while (!found) while (true)
{ {
if (arr[mid] == target) found = true; if (arr[mid] == target) break;
if (start == end && !found) return -1; if (start == end) return -1;
if (mid == end && !found) return -1; if (mid == end) return -1;
else if (arr[mid] < target) else if (arr[mid] < target)
{ {
start = mid + 1; start = mid + 1;