Algorithm/๐Ÿ“š Concept

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฒ”์œ„๋ฅผ ๋ฐ˜์”ฉ ์ขํ˜€๊ฐ€๋Š” ์ด์ง„ ํƒ์ƒ‰ - ํŒŒ์ด์ฌ(Python)

J1Yun 2021. 7. 31. 18:00
728x90

์ˆœ์ฐจ ํƒ์ƒ‰

  • ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ํŠน์ •ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ์ผ์น˜ํ•˜๋ฉด ํƒ์ƒ‰ ์ข…๋ฃŒ, ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹ค์Œ ์›์†Œ๋กœ ์ด๋™ํ•œ๋‹ค.
# ์ˆœ์ฐจ ํƒ์ƒ‰
def sequential_search(n, target, array):
    for i in range(n):
        if array[i] == target:
            return i+1  # ํ˜„์žฌ ์œ„์น˜ ๋ฐ˜ํ™˜ (-๋ฒˆ์งธ)


 array = ['S', 'T', 'A', 'R']  # ์ž„์˜ ์ง€์ • ๋ฆฌ์ŠคํŠธ
 print("๋ฆฌ์ŠคํŠธ ์† A์˜ ์œ„์น˜๋Š” " + sequential_search(len(array), 'A', array) + "์ž…๋‹ˆ๋‹ค.") # ํƒ์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ

 

์ด์ง„ ํƒ์ƒ‰

  • ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ๋ฒ”์œ„๋ฅผ ๊ณ„์† ์ค„์—ฌ๋‚˜๊ฐ€๋ฉฐ ์ฐพ์œผ๋ ค๋Š” ๋ฐ์ดํ„ฐ์™€ ๋ฒ”์œ„์˜ ์ค‘๊ฐ„์ ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ๋น„๊ตํ•ด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•„๊ฐ€๋Š” ๋ฐฉ๋ฒ•

์˜ˆ์‹œ) ์ •๋ ฌ๋œ 10๊ฐœ์˜ ๋ฐ์ดํ„ฐ ์ค‘์—์„œ ๊ฐ’์ด 4์ธ ์›์†Œ ์ฐพ๊ธฐ

  1. ์‹œ์ž‘์ ๊ณผ ๋์  ์‚ฌ์ด์˜ ์ค‘๊ฐ„์ ์„ ๊ตฌํ•œ๋‹ค. ์ค‘๊ฐ„์ ์ด ์‹ค์ˆ˜์ผ ๋•Œ๋Š” ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆฐ๋‹ค. ๊ทธ๋ฆผ์—์„œ ์‹œ์ž‘์ ์€ [0], ๋์ ์€ [9]์ด๋ฏ€๋กœ ์ค‘๊ฐ„์ ์€ [4]์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ค‘๊ฐ„์  [4]์˜ ๋ฐ์ดํ„ฐ 8๊ณผ ์ฐพ์œผ๋ ค๋Š” ๋ฐ์ดํ„ฐ 4๋ฅผ ๋น„๊ตํ•œ๋‹ค. ์ค‘๊ฐ„์ ์˜ ๋ฐ์ดํ„ฐ 8์ด ๋” ํฌ๋ฏ€๋กœ ๋์ ์„ [4]์˜ ์ด์ „์ธ [3]์œผ๋กœ ์˜ฎ๊ฒจ ๋ฒ”์œ„๋ฅผ ์ขํžŒ๋‹ค.
  2. ์‹œ์ž‘์  [0], ๋์  [3]์˜ ์ค‘๊ฐ„์  [1]์— ์œ„์น˜ํ•œ ๋ฐ์ดํ„ฐ 2๋Š” ์ฐพ์œผ๋ ค๋Š” ๋ฐ์ดํ„ฐ 4๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ์‹œ์ž‘์ ์„ [2]๋กœ ์˜ฎ๊ฒจ ๋ฒ”์œ„๋ฅผ ๋‹ค์‹œํ•œ๋ฒˆ ์ขํžŒ๋‹ค.
  3. ์‹œ์ž‘์  [2], ๋์  [3]์˜ ์ค‘๊ฐ„์  [2]์— ์œ„์น˜ํ•œ ๋ฐ์ดํ„ฐ 4๋Š” ์ฐพ์œผ๋ ค๋Š” ๋ฐ์ดํ„ฐ 4์™€ ๊ฐ™์œผ๋ฏ€๋กœ ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•œ๋‹ค.
# ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•œ ์ด์ง„ ํƒ์ƒ‰

def binary_search(array, target, start, end):
    while start <= end:
        mid = (start + end) // 2  # ์ค‘๊ฐ„์  ์ธ๋ฑ์Šค
        if array[mid] == target:
            return mid  # ํƒ์ƒ‰ ์™„๋ฃŒ (์ค‘๊ฐ„์  ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜)
        elif array[mid] > target:
            end = mid - 1  # ์ค‘๊ฐ„์ ์˜ ๊ฐ’๋ณด๋‹ค ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์ž‘์€ ๊ฒฝ์šฐ ๋์  ์กฐ์ •
        else:
            start = mid + 1  # ์ค‘๊ฐ„์ ์˜ ๊ฐ’๋ณด๋‹ค ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ํฐ ๊ฒฝ์šฐ ์‹œ์ž‘์  ์กฐ์ •
    return None  # ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ

# ์›์†Œ์˜ ๊ฐœ์ˆ˜(n)๊ณผ ์ฐพ๊ณ ์žํ•˜๋Š” ๊ฐ’(target) ์ž…๋ ฅ๋ฐ›๊ธฐ
n, target = map(int, input().split())
# ์ „์ฒด ์›์†Œ ์ž…๋ ฅ๋ฐ›๊ธฐ
array = list(map(int, input().split()))

# ์ด์ง„ ํƒ์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ
result = binary_search(array, target, 0, n-1)
if result == None:
    print("ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋Š” ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
else: 
    print(result + 1)

 

ํŒŒ์ด์ฌ ์ด์ง„ํƒ์ƒ‰ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Bisect

โ€ป ๋ฐ˜๋“œ์‹œ data๊ฐ€ ์‚ฌ์ „์— ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ ํ•จ

  • bisect.bisect\_left(data, x): data๋‚ด์— x๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ์˜(์ž‘์€) ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • bisect.bisect\_right(data, x): data๋‚ด์— x๊ฐ€ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์€(ํฐ) ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
import bisect

data = [1, 2, 2, 3, 4]
x = 2

print(bisect.bisect_left(data, x))  # 1
print(bisect.bisect_right(data, x))  # 3
728x90