Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 5582
- 5582 파이썬
- flow buffering
- 1753 다익스트라
- 1806 백준
- 백준 2096
- 자바
- 백준 1644
- Android Room
- 1806 투포인터
- 백준 10819
- 5582 DP
- 1753 파이썬
- 코루틴 플로우
- 자료구조
- 1644 파이썬
- 1003 파이썬
- android hilt
- 안드로이드 hilt
- 2096 파이썬
- 10819 파이썬
- java
- Android mvp
- 이진 탐색
- Jetpack Room
- git local remote
- 6588 파이썬
- 투포인터 알고리즘
- Coroutine Flow
- 1806 파이썬
Archives
- Today
- Total
Gemstone's Devlog
[Data Structure] 이진 탐색 알고리즘의 재귀적 구현 본문
#include <stdio.h>
int BSearchRecur(int ar[], int first, int last, int target)
{
int mid;
if (first > last) // 재귀함수의 탈출 조건
return -1; // -1의 반환은 탐색의 실패를 의미
mid = (first + last) / 2; // 탐색대상의 중앙을 찾는다.
if (ar[mid] == target)
return mid; // 탐색된 타겟의 인덱스 값 반환
else if (target < ar[mid])
return BSearchRecur(ar, first, mid - 1, target);
else
return BSearchRecur(ar, mid + 1, last, target);
}
int main(void)
{
int arr[] = { 1, 3, 5, 7, 9 };
int idx;
idx = BSearchRecur(arr, 0, sizeof(arr) / sizeof(int) - 1, 7);
if (idx == -1)
printf("탐색 실패 \n");
else
printf("타겟 저장 인덱스: %d \n", idx);
idx = BSearchRecur(arr, 0, sizeof(arr) / sizeof(int) - 1, 4);
if (idx == -1)
printf("탐색 실패 \n");
else
printf("타겟 저장 인덱스: %d \n", idx);
return 0;
}
'Data Structure & Algorithms' 카테고리의 다른 글
[백준] 1316번: 그룹 단어 체커 (0) | 2021.08.06 |
---|---|
[백준] 2908번: 상수 (0) | 2021.08.06 |
[백준] 1152번: 단어의 개수 (0) | 2021.08.06 |
[Data Structure] 하노이 타워 문제 해결 (0) | 2021.06.29 |
[Data Structure] 순차 탐색 / 이진 탐색 알고리즘의 연산횟수 비교 (0) | 2021.06.28 |