S = [-1,10,12,13,14,18,20,25,27,30,35,40,45]
x = 18
def location(S,low,high):
#배열, 처음 탐색 인덱스, 마지막 인덱스
if(low>high):
return 0
else:
mid = (low+high)//2
if (x == S[mid]):
return mid
elif (x < S[mid]):
return location(S,low,mid-1)
else :
return location(S,mid+1,high)
loc = location(S,1,len(S)-1)
print(loc)
반응형
'알고리즘' 카테고리의 다른 글
연구소 14502 BFS (1) | 2022.09.18 |
---|---|
적록색약 10026 -DFS (0) | 2022.09.18 |
아기상어 16236 BFS (1) | 2022.09.18 |
안전영역 -2468 (DFS) (1) | 2022.09.18 |
설탕 배달-2839 (while-else문) (0) | 2022.09.16 |