Created
May 26, 2015 16:20
-
-
Save macrat/931a74578bcf5cc8efff 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
#include <stdio.h> | |
void showList(char** list, size_t length) | |
{ | |
printf("%d: ", length); | |
for(int i=0; i<length; i++) | |
printf("%s, ", list[i]); | |
printf("\npivot: %s\n\n", list[length/2]); | |
} | |
char* finder(char* target, char** list, size_t length) | |
{ | |
showList(list, length); | |
if(length == 0) | |
return NULL; | |
int diff = strcmp(target, list[length/2]); | |
if(diff == 0) | |
return list[length/2]; | |
if(diff > 0) | |
return finder(target, &list[length/2 + 1], length/2 - 1 + length%2); | |
else | |
return finder(target, list, length/2); | |
} | |
int main() | |
{ | |
char target[] = "dave"; | |
char* list[] = {"alice", "bob", "charlie", "dave", "ellen"}; | |
printf("result = %s\n", finder(target, list, sizeof(list)/sizeof(char*))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment