ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 이진탐색
    공부/프로그래밍 2014. 5. 20. 10:30

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    #include <stdio.h>
    #define MAX_SIZE 10
     
    int main()
    {
        int num[MAX_SIZE] = {5,8,13,4,2,21,31,1,44,7};
        int i,j, cmp,first=0,last=MAX_SIZE-1,tmp,half;
     
        for(i=0;i<MAX_SIZE;i++)
        {
            for(j=0;j<MAX_SIZE-1;j++)
            {
                if(num[j]>num[j+1])
                {
                    tmp = num[j];
                    num[j]= num[j+1];
                    num[j+1] = tmp;
                }
            }
        }
     
        for(i=0;i<MAX_SIZE;i++)
            printf("%d ",num[i]);
        printf("\n");
     
        half = (first+last)/2;
        do{
            printf("input : ");
            scanf("%d",&cmp);
            do{
     
            
                if(num[half]==cmp)
                {
                    printf("find! at %d \n",half);
                    break;
                }
     
                if(num[half]<cmp)    
                    first = half+1;
                else if(num[half]>cmp)
                    last = half;
     
                half = (last+first)/2;
            }while(last>first);
     
            if(last<=first)
                printf("Can't find\n");
     
            first = 0;
            last = MAX_SIZE-1;
     
        }while(1);
     
     
     
        return 0;
     
        
    }
     


    '공부 > 프로그래밍' 카테고리의 다른 글

    Rust - cargo를 이용한 rust 프로젝트 관리 (Cargo 사용법)  (0) 2015.02.21
    Turtle  (0) 2014.06.24
    이진탐색  (0) 2014.05.20
    dll injector 관련 자료  (0) 2014.05.19
    folder access api  (0) 2014.05.14
    php login form Eg  (0) 2014.05.12

    댓글 0

Designed by Tistory.