조회 수 2033 추천 수 0 댓글 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
번호 분류 제목 글쓴이 날짜 조회 수
699 Develop [ios] 아이폰에서 진동(Vibrate) 기능 추가하기 hooni 2014.04.18 4476
698 Develop [ios] Sprite Kit & 사운드 재생시 백그라운드 진입시 앱이 비정상적으로 종료됨 hooni 2014.04.18 4494
697 Develop [js] jQuery 셀 병합 1 file hooni 2014.09.23 4513
696 Develop macOS에 node, npm 설치하기 (homebrew) file hooni 2021.11.06 4569
695 Develop [ios] URL 파라미터 파싱~ hooni 2014.05.12 4571
694 Develop [ios] UILabel with two different color text hooni 2024.12.14 4576
693 Develop [php][laravel] 초간단 MacOS에서 Laravel 개발 환경 구축 hooni 2017.12.15 4604
692 Develop [php][laravel] 라라벨 프로젝트 생성 및 구조 file hooni 2017.12.15 4610
691 Develop [ios] iOS에서 디바이스 종류 알아오기 hooni 2014.05.24 4615
690 Develop [ios] SQLite 사용하기(튜토리얼) + 샘플코드 file hooni 2014.03.28 4630
689 Develop [php] Laravel 4. twitter bootstrap 적용하기 hooni 2018.04.05 4673
688 Develop [php] Laravel Route에서 PC/Mobile 분기 hooni 2018.01.24 4677
687 Develop [ios] NavigationController 에서 왼쪽(back) 버튼 후킹하기 hooni 2015.10.23 4696
686 Develop [ios] NSString URL Encode/Decode (인코딩/디코딩) hooni 2014.05.02 4759
685 Develop [ios] 앱 딜리게이트 얻어오기. (AppDelegate) hooni 2014.05.10 4776
684 Develop [matlab] ZigZag-Scanning (2-D Array) file hooni 2016.10.15 4777
Board Pagination Prev 1 ... 8 9 10 11 12 ... 53 Next
/ 53