Contents

조회 수 838 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
133 Develop [php] 초간단 게시판 페이지 분할 알고리즘 hooni 2003.04.23 8699
132 Develop [vbs] CD롬 뱉는 스크립트.. hooni 2003.04.23 11699
131 Develop [js] 새로고침(refresh)방법과 다른 페이지 바꾸기.. hooni 2003.04.23 6521
130 Develop [c] 단기과정[01/24] 정렬 알고리즘 hooni 2003.04.23 6955
129 Develop [c] 단기과정[01/17] 후위연산 스택 계산기.. file hooni 2003.04.23 7226
128 Develop [c] 단기과정[01/15] 피보나치, 파스칼.. 링크리스트 file hooni 2003.04.23 7234
127 Develop [c] 단기과정[01/14] 피보나치, 파스칼.. 파일입출력 file hooni 2003.04.23 7081
126 Develop [c] 단기과정[01/14] 파일 입출력 file hooni 2003.04.23 6821
125 Develop [c] 단기과정[01/10] 과제.. swap(any data, ..) file hooni 2003.04.23 6904
124 Develop [c] 단기과정[01/08] 과제.. 파스칼 삼각형 file hooni 2003.04.23 7256
123 Develop [c] 단기과정[01/08] (다차원 + 배열)포인터, void 포인터 hooni 2003.04.23 7770
122 Develop [c] 단기과정[01/08] 배열, 포인터 hooni 2003.04.23 7223
Board Pagination Prev 1 ... 55 56 57 58 59 60 61 62 63 64 ... 71 Next
/ 71