Last active
August 29, 2015 14:02
-
-
Save walkingtospace/04e4dfc9b272fea1d795 to your computer and use it in GitHub Desktop.
binary search 를 응용한 주어진 값의 인덱스 찾기
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
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