Develop
2015.06.26 16:35
[c] 이진 탐색 두 가지 코드 (재귀/반복)
조회 수 839 댓글 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; }
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
325 | Develop | [java] 채팅창 처럼.. swing.. | hooni | 2013.04.23 | 8769 |
324 | Develop | [c] 압축 알고리즘 소스 및 정리 | hooni | 2013.04.23 | 8779 |
323 | Develop | [c++] 레지스트리 등록 예제 1 | hooni | 2013.04.23 | 8789 |
322 | Develop | [c++] mfc 이용한 트레이아이콘(TrayIcon) 클래스 예제 프로젝트 | hooni | 2013.04.23 | 8793 |
321 | Develop | [c] 함수 요약 (검색해서 쓰세요..) | hooni | 2003.04.23 | 8795 |
320 | Develop | [c] 지나가는 패킷 잡기 | hooni | 2003.04.23 | 8802 |
319 | Develop | [html] SVG(Scalable Vector Graphics) 간단 정리 | hooni | 2014.02.13 | 8831 |
318 | Develop | [c++] 윈도우 프로세스뷰어 분석.. ㅋㅋ | hooni | 2013.04.23 | 8838 |
317 | Develop | [js] 초간단 암호화/복호화 소스.. | hooni | 2003.04.23 | 8839 |
316 | Develop | [c] 약수/최대공약수/완전수 알고리즘 | hooni | 2003.04.23 | 8865 |
315 | Develop | [c] 최대공약수 알고리즘 (유클릿) | hooni | 2003.04.23 | 8873 |
314 | Develop | [js] jQjuery $ 활용 | hooni | 2013.12.17 | 8874 |