def binary_search(arr, val): # def 함수이름(매개변수): -> 함수선언
first = 0 # 시작점 지정 (인덱스 0부터)
last = len(arr) - 1 # 끝지점 지정, 인덱스는 0부터 시작이므로, 길이보다 1작아야 함
while first <= last: # 반복문이 배열의 끝까지 돌도록 조건 정할것
mid = (first + last) // 2 # 중간 지점 계산 정수 연산으로 구할것
if arr[mid] == val: # 중간인덱스의 값과 구하려는 변수 일치
return mid # 중간값 반환
elif arr[mid] > val: # 중간값 > 찾으려는 변수 라면?
last = mid - 1 # 중간값의 왼쪽 부분만 탐색하면 되므로
else:
first = mid + 1
return -1
이 코드를 한줄한줄 쉽게 설명해줘
To embed this project on your website, copy the following code and paste it into your website's HTML: