3.3. Search - 3.3.1 Binary Search
CS - 3. Common Algorithms - 3.3. Search - 3.3.1 Binary Search
Search Algorithms
π‘ Search algorithms are used to find a specific item in a collection of items. For example, if you have a list of names and you want to find a specific name, you can use a search algorithm to find that name.
κ²μ μκ³ λ¦¬μ¦μ νλͺ© λͺ¨μμμ νΉμ νλͺ©μ μ°Ύλ λ° μ¬μ©λ©λλ€. μλ₯Ό λ€μ΄, μ΄λ¦ λͺ©λ‘μ΄ μλλ° νΉμ μ΄λ¦μ μ°ΎμΌλ €λ κ²½μ° κ²μ μκ³ λ¦¬μ¦μ μ¬μ©νμ¬ ν΄λΉ μ΄λ¦μ μ°Ύμ μ μμ΅λλ€.
Binary Search
π‘ Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array.
μ΄μ§ κ²μμ μ λ ¬λ λ°°μ΄ λ΄μμ λμ κ°μ μμΉλ₯Ό μ°Ύλ κ²μ μκ³ λ¦¬μ¦μ λλ€. μ΄μ§ κ²μμ λμ κ°κ³Ό λ°°μ΄μ μ€κ° μμλ₯Ό λΉκ΅ν©λλ€. λμ΄ κ°μ§ μμΌλ©΄ λͺ©νκ°μ΄ λμΌ μ μλ μ λ°μ μ κ±°νκ³ λλ¨Έμ§ μ λ°μμ κ²μμ κ³μνμ¬ λ€μ μ€κ° μμλ₯Ό λͺ©νκ°κ³Ό λΉκ΅νκ³ λͺ©νκ°μ μ°Ύμ λκΉμ§ μ΄ κ³Όμ μ λ°λ³΅ν©λλ€. λλ¨Έμ§ μ λ°μ΄ λΉμ΄ μλ μνμμ κ²μμ΄ λλλ©΄ λμμ΄ λ°°μ΄μ μλ κ²μ λλ€.
Binary Search(μ΄μ§νμ)μ μ λ ¬λ λ°°μ΄μμ νΉμ κ°μ μ°Ύλ μκ³ λ¦¬μ¦μ λλ€.
μ΄ μκ³ λ¦¬μ¦μ λ°°μ΄μ μ€κ° μμμ μ°Ύκ³ μ νλ κ°μ λΉκ΅νμ¬ νμ λ²μλ₯Ό λ°μΌλ‘ μ€μ¬λκ°λ λ°©μμΌλ‘ λμν©λλ€.
μ΄ν΄λ₯Ό λκΈ° μν κ°λ¨ν μμλ₯Ό 보μ¬λλ¦¬κ² μ΅λλ€.
μμλ‘
λ°°μ΄ [1,3,5,7,9,11,13,15] μμ μ«μ 9 λ₯Ό μ°Ύλλ€κ³ κ°μ ν΄ λ΄ μλ€.
Binary Searchλ μ΄λ¬ν κ³Όμ μ κ±°μΉ©λλ€.
1. λ°°μ΄μ κ°μ΄λ° μμμΈ 7κ³Ό μ°Ύκ³ μ νλ κ° 9λ₯Ό λΉκ΅ν©λλ€.
2. 7μ 9λ³΄λ€ μκΈ° λλ¬Έμ, 7 μΌμͺ½μ μμΉν κ°λ€μ 9λ³΄λ€ μλ€κ³ νλ¨ν©λλ€.
λ°λΌμ 7μ ν¬ν¨ν μΌμͺ½λΆλΆμ νμ λ²μμμ μ μΈν©λλ€.
3. λ¨μ λ°°μ΄μ [9, 11, 13, 15] μ λλ€. μ΄μ λ€μ μ€κ° μμ 11κ³Ό 9λ₯Ό λΉκ΅ν©λλ€.
4. 11μ 9λ³΄λ€ ν¬κΈ° λλ¬Έμ, 11 μ€λ₯Έμͺ½μ μμΉν κ°λ€μ 9λ³΄λ€ ν¬λ€λ κ²μ μ μ μμ΅λλ€.
λ°λΌμ 11μ ν¬ν¨ν μ€λ₯Έ μͺ½ λΆλΆ λ°°μ΄μ νμ λ²μμμ μ μΈν©λλ€.
5. λ¨μ λ°°μ΄μ [9,] μ λλ€. λ°λΌμ μ€κ°μμμΈ 9μ μ°Ύκ³ μ νλ λμ 9κ° μΌμΉνλ―λ‘ μνλ κ°μ μ°Ύμμ΅λλ€.
μΆλ¦¬κ²μλ₯μμ μ¬λ¬ ꡬμ¬μ€ νλλ§ λ€λ₯Όλ μν μ μΈμ μ΅μ νμλ‘ μ¬μ©ν΄μ ꡬν λ μ¬μ©νλ λ Όλ¦¬μ λΉμ·νκ² μ κ°λ©λλ€.
νμ λ²μλ₯Ό λ°μΌλ‘ μ€μ¬κ°λ―λ‘, Binary Searchλ λμμ λΉ λ₯΄κ² μ°Ύμ μ μλ ν¨μ¨μ μΈ μκ³ λ¦¬μ¦μ λλ€.
In Python
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
μμ μμμμ arrμ μ λ ¬λ λ°°μ΄μ΄λ©°, targetμ μ°Ύκ³ μ νλ κ°μ λλ€.
low, high, midλ κ°κ° νμ λ²μμ μμ, λ, μ€κ° μ λλ€.
μμμ λ§νλ λλ‘ target κ°μ mid(μ€κ°)μ λΉκ΅νκ³ +-1 μ νλ©° νμν©λλ€.
ν¨μλ μ°Ύκ³ μ νλ κ°μ μΈλ±μ€λ₯Ό λ°ννλ©°, λ§μ½ κ°μ΄ λ°°μ΄μ μμ κ²½μ° -1μ λ°νν©λλ€.
Binary Searchλ νΉμ±μ νμ λμμ΄ μ λ ¬λμνμ¬μΌλ§ μ¬μ©ν μ μμ΅λλ€.
μ΄λ₯Ό μν΄ λ―Έλ¦¬ λ°°μ΄μ μ λ ¬ν΄μΌ ν©λλ€.
μκ°λ³΅μ‘λλ O(log n)μΌλ‘ λ§€μ° ν¨μ¨μ μ΄κΈ° λλ¬Έμ, ν¬κΈ°κ° ν° μ λ ¬λ λ°°μ΄μμ μνλ κ°μ λΉ λ₯΄κ² μ°Ύμλ νμ©νκΈ° μ’μ΅λλ€.
(μ¬μ μμ λ¨μ΄λ₯Ό μ°Ύλκ²½μ°, DBμμ νΉμ λ μ½λλ₯Ό μ°Ύλ κ²½μ°λ±λ±)