Develop
2015.06.26 16:35
[c] 이진 탐색 두 가지 코드 (재귀/반복)
조회 수 842 댓글 0
첨부 '1' |
---|
이진 탐색에 대한 두 가지 코드.
# 실행 조건
- 유일한 값들이어야 함 (중복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; }
-
삼성페이(Samsung Pay) 구조
-
[android] Android N requires the IDE to be running with Java 1.8 or later 오류
-
[c] 파일명 또는 특정 패턴을 적용
-
[ios] Crashlytics, Fabfic 설치/설정
-
[ios] 새로 만들고 있는 DateMemo
-
영어. 반드시 외워야 할 영어 숙어
-
영어. 반드시 외워야 할 단어
-
[ios][swift] 초간단 카달로그 앱 (Catalog App)
-
[ios][swift] 초간단 To-do Memo
-
[ios][swift] 초간단 TableView 샘플
-
[ios] 웹뷰 history.back() ㅋㄷ
-
플라스터(Plaster) 수업 내용