i wrote the helpers.c which seems correct to me but every time I check with cs50, it tells me server was killed, help is appreciated.
include
include "helpers.h"
/** * Returns true if value is in array of n values, else false. */ bool search(int value, int values[], int n) {
int lower = 0;
int upper = n -1;
// checking whether n less than zero
if (n < 0)
{
return false;
}
// implementing the binary search tree
while(lower <= upper)
{
// getting the midpoint of the array index
int midpoint = (upper - lower) / 2;
// checking whether the array's midpoint value is greater than user value.
if (values[midpoint] > value)
{
// our upperbound is now updated to half of the original array
upper = midpoint - 1;
}
// same logic as above
else if (values[midpoint] < value)
{
// our lowerbound is now updated to half of original array
lower = midpoint + 1;
}
else if (values[midpoint] == value)
{
return true;
}
else
{
return false;
}
}
return 0;
}
/** * Sorts array of n values. */ void sort(int values[], int n) { // Bubble sort: implement an O(n^2) sorting algorithm
int counter;
do
{
counter = 0;
// iterate through the array
for(int i = 0; i < n; i++)
{
// checking whether the current value greater than next value
if (values[i] > values[i+1])
{
// swapping the neighbour values
int temp = values[i+1];
values[i + 1] = values[i];
values[i] = temp;
counter++;
}
}
// once counter stays at zero we know the list is sorted :)
}while(counter > 0);
return;
}