Contents

조회 수 839 댓글 0
Atachment
첨부 '1'
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

이진 탐색에 대한 두 가지 코드.


# 실행 조건

- 유일한 값들이어야 함 (중복x).

- 오름차순 정렬 후 실행해야 함.


# 구현 방식

- 재귀함수 (bsearch_recursive)

- while 반복문 (bsearch_loop)


#include <stdio.h>
#include "bsearch.c"

void bbsort(int *arr, int length);
int bsearch_recursive(int *arr, int begin, int end, int target);
int bsearch_loop(int *arr, int target, int length);

int main( )
{
    int arr[] = {11, 9, 1, 5, 15, 3, 7, 13};
    int target = 7;
    int result;
    int length;
    
    length = sizeof(arr)/sizeof(int);
    
    bbsort(arr, length);
    
    //By Recursive
    result = bsearch_recursive(arr, 0, length-1, target);
    
    //By Loop
    result = bsearch_loop(arr, length, target);
    
    if(result == -1)
    {
        printf("Not Found.
");
    }
    else
    {
        for( int i=0 ; i<length ; ++i )
        {
            printf( "%d ", arr[i] );
        }
        printf("
Found Index :  %d.
", result);
    }
    
    return 0;
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
325 Develop [android] 만화 어플 소스코드 file hooni 2013.04.23 92836
324 Develop [c#] 웹문서 소스(html) 긁어오기 file hooni 2013.04.23 24506
323 Develop [C#] MD5, SHA1 해시 & 인코딩 hooni 2013.04.23 77855
322 Develop [spring] 스프링 IoC/DI hooni 2013.04.23 11347
321 Develop [java] Interface 와 abstract hooni 2013.04.23 8704
320 Develop [ios] 코코아 프레임워크(Cocoa Framework) 기본적인 내용~ hooni 2013.04.23 27682
319 Develop [ios] 참고할만한 좋은 예제 소스.. hooni 2013.04.23 27371
318 Develop [js] 수학 공식을 제공하는 Math 객체 hooni 2013.04.23 15412
317 Develop [js] 간단한 게임 프로토타입 (HTML5 와는 무관) hooni 2013.04.23 15152
316 Develop [android] 자동차 리모컨 소스코드 secret hooni 2013.04.23 17082
315 Develop [android] [번역] 안드로이드 Android Cloud to Device Messaging(C2DM) hooni 2013.04.23 20421
314 Develop [ios] Objective-C 문자열 조작 메서드 hooni 2013.04.23 26455
Board Pagination Prev 1 ... 39 40 41 42 43 44 45 46 47 48 ... 71 Next
/ 71