Contents

조회 수 2021 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
519 Develop [c] 도메인 소켓(Unix Domain Socket) UDP file hooni 2013.04.23 10173
518 Develop [c] 디피-헬만 키교환(Diffie–Hellman key exchange) 샘플 코드.. ㅋㄷ file hooni 2013.04.23 10127
517 Develop [php] 초간단 게시판 페이지 분할 알고리즘 hooni 2003.04.23 10116
516 Develop [js] 폼(form) 전송시 중복 클릭 방지 간단한 구문 hooni 2013.04.23 10112
515 Develop [c] 파일(File)의 연결과 연결 해제 (link, unlink, chown) hooni 2013.04.23 10103
514 Develop [c] 공용체를 이용해 MSB를 LSB로 변환 file hooni 2013.04.23 10090
513 Develop [c] OpenGL 직사각형(2D) 크기 확대/축소 hooni 2003.04.23 10087
512 Develop [c][cpp] mfc, win32api, 예제 소스 대박 모음~ file hooni 2013.04.23 10026
511 Develop [c][java] 주사선 채우기 알고리즘(scan line filling algorithm) 구현 file hooni 2013.04.23 10026
510 Develop [c] OpenGL 마우스 이벤트 hooni 2003.04.23 10021
509 Develop [java] 채팅창 처럼.. swing.. file hooni 2013.04.23 9998
508 Develop [c] 컴파일러 DFA구현^^ 입력 받아 실행 4 file hooni 2003.04.23 9976
Board Pagination Prev 1 ... 23 24 25 26 27 28 29 30 31 32 ... 71 Next
/ 71