I have binary search - this one , but i need to change it like this , that it will search : for example we have array {1,10,11,12,15,16,22,22,22,22,30,40} I want to find 22 but not randomly - or it will be first 22(that goes after 16) or last 22 (that before 30)
int binary_search(int search_key, int list_size, const int array_to_search[], char success[]) {
int first, last, mid, index;
strcpy(success, "FALSE");
index = -1;
first=0;
last=list_size-1;
while (first <= last && strcmp(success, "FALSE") == 0)
{
mid = (first +last) / 2;
if (array_to_search[mid] == search_key)
{
index=mid;
strcpy(success, "TRUE");
}
else if (array_to_search[mid] > search_key ) {last = mid-1;}
else first = mid +1;
}
return index;
}
Thank u!!!!