Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool search(int value, int values[], int n)
- {
- for (int i = 0; i < n; i++)
- {
- int start_point = values[0], end_point = values[n - 1];
- if (start_point > end_point)
- {
- // value doesn't exist in the array, so return false
- // to mean we didn't find it
- return false;
- }
- int midpoint = 0;
- if (n % 2 != 0)
- {
- midpoint = floor((start_point + midpoint) / 2);
- }
- else
- {
- int middle = floor((start_point + midpoint) / 2);
- midpoint = floor(((middle + 2) + (middle - 2)) / 2);
- }
- if (values[midpoint] == value)
- {
- return true;
- }
- else
- {
- if (value > values[midpoint])
- {
- start_point = midpoint + 1;
- }
- else if (value < values[midpoint])
- {
- end_point = midpoint - 1;
- }
- /*if (values[i] == value)
- {
- return true;
- }*/
- }
- }
- return false;
- }
Add Comment
Please, Sign In to add comment