I'm working on pset3, and I think I've finished helpers.c
. It sorts correctly, and I can't find any mistakes, but it does something really weird. If I enter the values 1
, 2
, 3
, 4
, 5
into find.c
as hay, it sorts it, except it ends up as 0
,1
, 2
, 3
, 4
. There's always a number missing and a 0
to replace it.
I use this code to sort:
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm
int track = n;
int check = 1;
int hold;
while (check != 0)
{
check = 0;
for (int i = 0; i < track; i++)
{
if (values[i] > values[i + 1])
{
check++;
hold = values[i];
values[i] = values[i + 1];
values[i + 1] = hold;
}
}
track--;
}
And I use this code to search:
bool search(int value, int values[], int n)
{
// TODO: implement a searching algorithm
int min = 0;
int max = n;
int x;
while (max - min > 1)
{
x = round((max + min) / 2);
if (values[x] < value)
min = x;
else if (values[x] > value)
max = x;
else
return true;
}
return false;
}
Can anyone please help me figure out what's wrong?