Last active
December 20, 2015 00:58
-
-
Save GZShi/6045133 to your computer and use it in GitHub Desktop.
二分搜索,正确性未知……
This file contains 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
typedef int type; | |
int my_binary_search(type *array, int begin, int end, type key) { | |
int mid = 0; | |
if(array == NULL || begin > end || begin < 0) | |
return -1; | |
else | |
mid = begin + ((end - begin) >> 2); | |
if(array[mid] == key) | |
return mid; | |
else if(array[mid] > key && mid > begin) | |
return my_binary_search(array, begin, mid - 1, key); | |
else if(array[mid] < key && mid < end) | |
return my_binary_search(array, mid + 1, end, key); | |
else | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment