Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Last active August 29, 2015 14:02
Show Gist options
  • Save walkingtospace/04e4dfc9b272fea1d795 to your computer and use it in GitHub Desktop.
Save walkingtospace/04e4dfc9b272fea1d795 to your computer and use it in GitHub Desktop.
binary search 를 응용한 주어진 값의 인덱스 찾기
int findNeedle(int input[], int start, int end, int needle) {
if(start > end) return -1;
int mid = floor((start+end)/2);
int result;
if(input[mid] == needle) return mid;
else if(input[mid] < needle) {
result = findNeedle(input, mid+1, end, needle);
if(result == -1) {
return end+1;
}
}
else {
result = findNeedle(input, start, mid-1, needle);
if(result == -1) {
return start;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment