Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ae432b578f3dae049c98 to your computer and use it in GitHub Desktop.
Save anonymous/ae432b578f3dae049c98 to your computer and use it in GitHub Desktop.
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