알고리즘

이분검색

토니짱 2022. 9. 22. 16:29
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)

 

 

반응형