Skip to content

Instantly share code, notes, and snippets.

Created Mar 1, 2015
Embed
What would you like to do?
bool search(int value, int values[], int n)
{
int lower = 0;
int upper = n;
int middle;
printf ("start search");
do
{
middle = (lower + upper) / 2;
if (values [middle] == value)
{
return true;
break;
}
if (values [middle] < value)
lower = middle + 1;
else
upper = middle - 1;
}
while (middle != upper);
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
int counter = 0;
int swap = 0;
do
{
swap = 0;
for (int i = 0; i < n - 1 - counter; i++)
{
int temp;
for (int j = 0; j < n; j++)
printf ("%d ", values [j]);
// compare values
if (values[i] > values[i + 1])
{
swap = 1;
temp = values [i];
values[i] = values[i + 1];
values[i + 1] = temp;
}
}
counter ++;
}
while (swap != 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment