[Python] ์ด๋ถ„ ํƒ์ƒ‰
ยท
Algorithm/Python
์ด๋ถ„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ def binary_search(a, x): start = 0 end = len(a) - 1 while start a[mid]: start = mid + 1 else: end = mid - 1 return -1 d = [1, 4, 9, 16, 25, 36, 49, 64, 81] print(binary_search(d, 36)) print(binary_search(d, 50)) #1 ์ค‘๊ฐ„ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค. #2 ์ฐพ๋Š” ๊ฐ’๊ณผ ์ค‘๊ฐ„ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค. #3 ๊ฐ™๋‹ค๋ฉด ์œ„์น˜๋ฒˆํ˜ธ๋ฅผ ๋Œ๋ ค์ฃผ๊ณ  #4 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์˜ค๋ฅธ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ) #5 ์ฐพ๋Š” ๊ฐ’์ด ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ค‘๊ฐ„ ์œ„์น˜์˜ ์™ผ์ชฝ์„ ๋Œ€์ƒ์œผ๋กœ ๋‹ค์‹œ ํƒ์ƒ‰(1๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ)