Skip to content

Instantly share code, notes, and snippets.

@GZShi
Last active December 20, 2015 00:58
Show Gist options
  • Save GZShi/6045133 to your computer and use it in GitHub Desktop.
Save GZShi/6045133 to your computer and use it in GitHub Desktop.
二分搜索,正确性未知……
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