File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .*;
2+
3+ public class Main {
4+
5+ // 이진 탐색 소스코드 구현(재귀 함수)
6+ public static int binarySearch (int [] arr , int start , int end ) {
7+ if (start > end ) return -1 ;
8+ int mid = (start + end ) / 2 ;
9+ // 고정점을 찾은 경우 중간점 인덱스 반환
10+ if (arr [mid ] == mid ) return mid ;
11+ // 중간점의 값보다 중간점이 작은 경우 왼쪽 확인
12+ else if (arr [mid ] > mid ) return binarySearch (arr , start , mid - 1 );
13+ // 중간점의 값보다 중간점이 큰 경우 오른쪽 확인
14+ else return binarySearch (arr , mid + 1 , end );
15+ }
16+
17+ public static void main (String [] args ) {
18+ Scanner sc = new Scanner (System .in );
19+
20+ int n = sc .nextInt ();
21+ int [] arr = new int [n ];
22+ for (int i = 0 ; i < n ; i ++) {
23+ arr [i ] = sc .nextInt ();
24+ }
25+
26+ // 이진 탐색(Binary Search) 수행
27+ int index = binarySearch (arr , 0 , n - 1 );
28+
29+ // 결과 출력
30+ System .out .println (index );
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments