Skip to content

Instantly share code, notes, and snippets.

@mlcollard
Last active March 15, 2021 19:30
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 mlcollard/a2111503d37b6c54d4f217c49f49ad94 to your computer and use it in GitHub Desktop.
Save mlcollard/a2111503d37b6c54d4f217c49f49ad94 to your computer and use it in GitHub Desktop.
int position(int key, int elemArray[]) {
int bottom = 0;
int top = elemArray.length - 1;
while (bottom <= top) {
int midpoint = (top + bottom) / 2;
// found it
if (key == elemArray[midpoint])
return midpoint;
// top half
if (key < elemArray[midpoint])
bottom = midpoint + 1;
// bottom half
else
top = midpoint - 1;
}
// not in the array
return -1;
}
int position(int key, int elemArray[]);
void sort(double n[], int size);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment