I've been working on this for a while now, and I can't for the life of me figure out why the needle can't be found. In gdb when I run the function the second for loop with 'j' only appears once. Does this mean that it is not repeating?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include "helpers.h"
/**
* BINARY SEARCH FUNCTION
**/
bool search(int value, int values[], int n)
{
if (n<1)
{
return false;
}
// TODO: implement a detecting algorithm, if found end loop
int max=n-1;
int min=0;
while ((max-min)>=0)
{
int mid = ((min+max)/2);
if (values[mid]==value)
return true;
else if (values[mid]<value)
min = mid+1;
else if (values[mid]>value)
max = mid-1;
}
return false;
}
/**
* SELECTION SORT FUNCTION.
*/
void sort(int values[], int n)
{
for (int i=0; i<(n-1); i++)
{
int min = i;
for (int j=i+1; j<n; j++)
{
if (values[j]<values[min])
{
min = j;
}
}
int temp = values[i];
values[i] = values[min];
values[min] = temp;
}
}