본문 바로가기

개발

Binary Search

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int binary_search(int n, char* str, char target){
    int begin = 0;
    int end = n - 1;
    int mid, result;
    while(begin <= end){
        mid = (begin + end/ 2;
        result = strcmp(str[mid], target);
        if(result == 0)
            return str[mid];
        else if (result < 0)
            begin = mid + 1;
        else 
            end = mid - 1;
    }
    return -1;
}
cs