Contents

조회 수 2023 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
327 Develop [php] 초간단 게시판 페이지 분할 알고리즘 hooni 2003.04.23 10117
326 Develop [c] 디피-헬만 키교환(Diffie–Hellman key exchange) 샘플 코드.. ㅋㄷ file hooni 2013.04.23 10127
325 Develop [c] 도메인 소켓(Unix Domain Socket) UDP file hooni 2013.04.23 10173
324 Develop [c++] MFC로 만든 디렉토리/파일 파인더 file hooni 2013.04.23 10177
323 Develop [ios] UUID 생성 + Key Chain 연동 file hooni 2016.05.13 10187
322 Develop [js] jQjuery $ 활용 hooni 2013.12.17 10189
321 Develop [java] RGB 색상 조절 버튼.. 스윙(swing) file hooni 2013.04.23 10207
320 Develop 다운 받아서 테스트 해볼것.. hooni 2013.04.23 10237
319 Develop [c++] mfc이용한 트레이아이콘(TrayIcon) 클래스 예제 프로젝트 file hooni 2013.04.23 10254
318 Develop [js] jQuery 셀랙터(selector) 요약 hooni 2013.12.17 10266
317 Develop [c] 약수/최대공약수/완전수 알고리즘 hooni 2003.04.23 10271
316 Develop [c] 다중연결 서버 만들기 #2 - select() 사용 file hooni 2013.04.23 10287
Board Pagination Prev 1 ... 39 40 41 42 43 44 45 46 47 48 ... 71 Next
/ 71