CS - Roadmap.sh/3. Common Algorithms

3.3. Search - 3.3.1 Binary Search

ParkParksu 2024. 1. 23. 20:47

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μ—μ„œ νŠΉμ • λ ˆμ½”λ“œλ₯Ό μ°ΎλŠ” κ²½μš°λ“±λ“±)

728x90