이진 검색 | 이진 탐색 트리 |
---|---|
알고리즘 | 자료구조 |
정렬된 배열에서 값을 찾는 것 | 정렬된 구조를 저장하고 탐색 |
function binary_search(A, n, T) is
L := 0
R := n − 1
while L ≤ R do
m := floor((L + R) / 2)
if A[m] < T then
L := m + 1
else if A[m] > T then
R := m − 1
else:
return m
return unsuccessful
function binary_search_alternative(A, n, T) is
L := 0
R := n − 1
while L != R do
m := ceil((L + R) / 2)
if A[m] > T then
R := m − 1
else:
L := m
if A[L] = T then
return L
return unsuccessful
mid = (left + right) // 2
left + right
의 오버플로를 막으려면 right - left
를 사용mid = left + (right - left) // 2