# pset 3 - killed by server problem

I understand my problem is with infinite loops but I would like some guidance on solving it. I am not sure if I should use a for - as I tried doing on comments - or if there is a better/more efficient approach. Comments on other problems on the code are also accepted. Thanks!

``````#include <cs50.h>

#include "helpers.h"

/**
* Returns true if value is in array of n values, else false.
*/

bool search(int value, int values[], int n)
{

// if input is less than 2, it doesn't form a pair
if (n < 0)
{
return false;
}

// beggining and final portions
int min = 0;
int max = n -1;

// middle variable
int middle = n/2;

while (n > 0)
{

// iterate over the values
//for (int k = 0; k<(n-1); k++)
// {

// }

if (value > values[middle])
{
min = middle + 1;
}

else if (value < values[middle])
{
max = middle - 1;
}

else if (value == values[middle])
{
return true;
}
}

// if "needle" can't be found return false
return false;
}

/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int min;
int swap;

for (int i = 0; i<(n-2); i++)
{
min = i;

// loop to find smallest element
// check every value of j with the previous min (i)
for (int j = 0; j<(n-1); j++)
{
if (values[j] < values[min])
{
min = j;
}
}

// swap
if (min !=i)
{
swap = values[i];
values [i] = values[min];
values[min] = swap;
}

}
return;
}
``````