Created
March 30, 2018 03:03
-
-
Save zcwang/e1262079a7084235f487a950e9361ec3 to your computer and use it in GitHub Desktop.
index of array with its value...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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