์ƒˆ์†Œ์‹

๐Ÿงฎ PS

์ด์ง„ ํƒ์ƒ‰, ์ด๋ถ„ ํƒ์ƒ‰ (Binary serach) ์žฌ๊ท€

  • -

* ์ด์ง„ ํƒ์ƒ‰ (Binary serach) ๊ฐœ๋…

 

ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์— ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ˆœ์ฐจํƒ์ƒ‰(linear search)์€ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฐพ์„ ๋Œ€์ƒ์ด ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ๊ฒ€์ƒ‰ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด์ง„ ํƒ์ƒ‰์€ ํ•„์š” ์—†๋Š” ๋ถ€๋ถ„์„ ๋ฐฐ์ œํ•˜๋ฉฐ ๊ฒ€์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ค„์ด๋ฉด์„œ ์ง„ํ–‰ํ•˜๋Š” ํƒ์ƒ‰.

 

 

์ฒ˜์Œ ์ด์ง„ํƒ์ƒ‰์˜ ๊ฐœ๋…์„ ์ฝ๊ณ , ์ƒ๊ฐ๋‚˜๋Š”๊ฒƒ์€ ์†Œ์ฃผ๋šœ๊ป‘์œผ๋กœ ํ•˜๋˜ UP-DOWN ๊ฒŒ์ž„์ด์—ˆ๋‹ค. 

์†Œ์ฃผ๋šœ๊ป‘์—๋Š” 1 ~ 50๊นŒ์ง€์˜ ์ˆซ์ž๊ฐ€ ์žˆ๋Š”๋ฐ, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ์ˆซ์ž๋ฅผ ๋งž์ถœ ํ™•๋ฅ ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์ค‘๊ฐ„๊ฐ’์„ ๊ณจ๋ผ๊ฐ€๋ฉฐ ๋ฒ”์œ„๋ฅผ

์ค„์—ฌ๋‚˜๊ฐ”๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ๋งž์ถ”๋Š” ์‚ฌ๋žŒ์ด 25๋ฅผ ์™ธ์น˜๋ฉด, ์ถœ์ œ์ž๋Š” UP ์ด๋‚˜ Down์„ ๋งํ•˜์—ฌ ๋ฒ”์œ„๋ฅผ ๋งํ•ด์ค€๋‹ค.

ํƒ์ƒ‰๋ฒ”์œ„ : 1 ~ 50

๋งŒ์•ฝ UP์„ ๋งํ•ด์ฃผ์—ˆ๋‹ค๋ฉด ๋‹ค์Œ ์ˆœ์„œ๋•Œ๋Š” 1~25๊นŒ์ง€์˜ ๋ฒ”์œ„๋Š” ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†๋Š”์…ˆ์ด ๋œ๋‹ค.

1์ฐจ ์‹œ๋„ ํ›„ ํƒ์ƒ‰๋ฒ”์œ„ : 26  ~  50

๋‹ค์Œ์œผ๋กœ 2์ฐจ ์‹œ๋„๋•Œ, 40์„ ์™ธ์ณค๊ณ  ์ถœ์ œ์ž๋Š” DOWN์„ ์™ธ์ณค๋‹ค๋ฉด ๋‹ค์Œ ํƒ์ƒ‰๋ฒ”์œ„๋Š”

2์ฐจ ์‹œ๋„ ํ›„ ํƒ์ƒ‰๋ฒ”์œ„ : 26  ~  39

์ด๋Ÿฐ์‹์œผ๋กœ ํƒ์ƒ‰ ๋ฒ”์œ„๊ฐ€ ์ ์ฐจ ์ค„์–ด๋“ ๋‹ค.

 


* ์ด์ง„ํƒ์ƒ‰์˜ ์ฝ”๋“œ ๊ตฌํ˜„

 

  • ์‹œ๊ฐ„๋ณต์žก๋„ BigO : O(log N)
  • ์ „์ œ์กฐ๊ฑด : ๋ฐ˜๋“œ์‹œ ์ •๋ ฌ๋œ ์ž๋ฃŒ์—ฌ์•ผ ํ•œ๋‹ค. ๋ฌด์ž‘์œ„ ์ˆœ์„œ๋ผ๋ฉด ์ด์ง„ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
  • ์ด์ง„ํƒ์ƒ‰์€ ์žฌ๊ท€(Recursive)์ ์œผ๋กœ๋„ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค. ๊ฐ™์€ ์—ฐ์‚ฐ์˜ ๋ฐ˜๋ณต์ด๊ธฐ๋•Œ๋ฌธ์—..

(bigocheatsheet.com ์˜ ์ฐจํŠธ์— ๋”ฐ๋ฅด๋ฉด ์ด์ง„ํƒ์ƒ‰์€ Excellentํ•œ ํšจ์œจ์ด๋‹ค.)

 

  • ํƒ์ƒ‰ํ•  ๋Œ€์ƒ(๋ฐฐ์—ด) array
  • ํƒ์ƒ‰ ๋ฒ”์œ„์˜ ์‹œ์ž‘๊ณผ ๋์„ ๋‚˜ํƒ€๋‚ด๋Š” start , end ํ˜น์€ left , right
  • ํƒ์ƒ‰ ๋ฒ”์œ„์˜ ์ค‘๊ฐ„๊ฐ’์ธ mid
  • ํƒ์ƒ‰ ๋ชฉํ‘œ์ธ target
์ด์ง„ํƒ์ƒ‰์˜ ๊ตฌํ˜„
target = 15

arr = [i for i in range(100)]
arr.sort()  # ์‚ฌ์‹ค ์—†์–ด๋„ ๋œ๋‹ค. for๋ฌธ์œผ๋กœ arr๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ๋ผ์„œ
print(arr)
start = 0 
end = len(arr) - 1

cnt = 0
while start <= end:
    cnt +=1

    mid = (start + end) // 2
    if arr[mid] == target:
        print("%d์ฐจ ์‹œ๋„ ,ํƒ์ƒ‰ ์™„๋ฃŒ !! ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ :%d" %(cnt, mid))
        break
    elif arr[mid] > target:
        end = mid - 1
        print("%d์ฐจ ์‹œ๋„ :"%cnt, start,end)
    else: 
        start = mid +1
        print("%d์ฐจ ์‹œ๋„ :"%cnt, start,end)

 

 

์ด์ง„ํƒ์ƒ‰ ์žฌ๊ท€์  ๊ตฌํ˜„

 

def binarySearch(arr, start, end, target):
    global cnt
    mid = (start + end)//2
    mid_value = arr[mid]
    cnt += 1
    if target == mid_value:
        print("%d์ฐจ ์‹œ๋„ , ํƒ์ƒ‰ ์™„๋ฃŒ !! ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ:%d"%(cnt, mid+1))
        
    elif mid > target:
        end = mid-1
        print("%d์ฐจ ์‹œ๋„ :"%cnt,start,end)
        binarySearch(arr, start, end, target)
        
    elif mid < target:
        start = mid +1
        print("%d์ฐจ ์‹œ๋„  :"%cnt, start, end)
        binarySearch(arr, start, end, target)
        
    else:
        return False



arr = [i for i in range(100)]
arr.sort()
target = 15
start = 0
end = len(arr) -1 
cnt = 0
binarySearch(arr, start, end, target)

 

 

 

 

์ด์ง„ ํƒ์ƒ‰ ๊ตฌํ˜„, ์žฌ๊ท€์  ๊ตฌํ˜„์˜ ๊ฒฐ๊ณผ๊ฐ’ (๊ฐ™์Œ)

*๊ฒฐ๊ณผ๊ฐ’ (์ถœ๋ ฅ ๋‚ด์šฉ)

1์ฐจ ์‹œ๋„ : 0 48
2์ฐจ ์‹œ๋„ : 0 23
3์ฐจ ์‹œ๋„  : 12 23
4์ฐจ ์‹œ๋„ : 12 16
5์ฐจ ์‹œ๋„  : 15 16
6์ฐจ ์‹œ๋„ , ํƒ์ƒ‰ ์™„๋ฃŒ !! ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ:16

 

 

 

 

 

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.