Skip to content

Instantly share code, notes, and snippets.

@zcwang
Created March 30, 2018 03:03
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 zcwang/e1262079a7084235f487a950e9361ec3 to your computer and use it in GitHub Desktop.
Save zcwang/e1262079a7084235f487a950e9361ec3 to your computer and use it in GitHub Desktop.
index of array with its value...
int index_search(int x[], int n) {
int first = 0;
int last = n - 1;
int middle, index;
index = -1;
while (first <= last) { /* a modified binary search*/
middle = (first + last) / 2;
if (x[middle] == middle) {
index = middle;
break;
} else if (x[middle] > middle) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment