티스토리 뷰


#include<stdio.h>

int BinarySearch(int *array, unsigned num, int key);

int BinarySearch(int *array, unsigned num, int key){
        unsigned Upper, Lower, Mid;
        Lower = 0;
        Upper = num - 1;
        while(1){
                Mid = (Upper + Lower)/2;
                if(array[Mid] == key) return Mid;
                if(array[Mid] > key){
                        Upper = Mid -1;
                }else{
                        Lower = Mid + 1;
                }
                if(Upper <= Lower){
                        return -1;
                }
        }
}
int main(void){
        int array[] = {2,6,13,19,21,21,23,29,35,48,62,89,90,95,99,102,109,208,629};
        unsigned num;
        int key, index;
        num = sizeof(array)/sizeof(array[0]);
        printf("ARRAY SIZE :%d   ARRAY[0] SIZE :%d     NUM : %d\n",sizeof(array),sizeof(array[0]),num);
        key = 29;
        index = BinarySearch(array, num, key);
        if(index == -1){
                puts("No Search Key");
        }else{
                printf("찾는 값은 %d번째 있습니다.\n",index);
        }
}

'Developer' 카테고리의 다른 글

redmine  (0) 2012.03.26
Jboss Clustering  (0) 2012.03.19
Apache CGI 연동  (0) 2012.03.19
Apache2.2 + SSL 설치  (0) 2012.03.15
Name Server Install & Zone file Setting  (0) 2012.03.09
댓글