File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# 이진 탐색 소스코드 구현 (재귀 함수)
2- def binary_search (start , end , target , array ):
2+ def binary_search (array , target , start , end ):
33 if start > end :
44 return None
55 mid = (start + end ) // 2
6+ # 찾은 경우 중간점 인덱스 반환
67 if array [mid ] == target :
78 return mid
9+ # 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
810 elif array [mid ] > target :
9- return binary_search (start , mid - 1 , target , array )
11+ return binary_search (array , target , start , mid - 1 )
12+ # 중간점의 값보다 찾고자 하는 값이 작은 경우 오른쪽 확인
1013 else :
11- return binary_search (mid + 1 , end , target , array )
14+ return binary_search (array , target , mid + 1 , end )
1215
1316# n(원소의 개수)과 target(찾고자 하는 문자열)을 입력 받기
1417n , target = list (map (int , input ().split ()))
1518# 전체 원소 입력 받기
1619array = list (map (int , input ().split ()))
1720
1821# 이진 탐색 수행 결과 출력
19- result = binary_search (0 , n - 1 , target , array )
22+ result = binary_search (array , target , 0 , n - 1 )
2023if result == None :
2124 print (None )
2225else :
You can’t perform that action at this time.
0 commit comments