3.3. Search - 3.3.1 Binary Search

2024. 1. 23. 20:47ใ†CS - Roadmap.sh/3. Common Algorithms

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

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.4. Cache - 3.4.1 LRU Cache  (2) 2024.01.25
3.3. Search - 3.3.2 Linear Search  (1) 2024.01.24
3.2. Recursion - 3.2.2 Non-Tail Recursion  (0) 2024.01.22
3.2. Recursion - 3.2.1 Tail Recursion  (0) 2024.01.21
3.1. Sorting - 3.1.6 Merge Sort  (0) 2024.01.20