๋ชฉ๋กbinarySearch (2)

suvera-dev ๐Ÿฅฆ

Algorithm) Binary Search

์ด์ง„ํƒ์ƒ‰์— ๋“ค์–ด๊ฐ€๊ธฐ์ „ ๊ฐ€์žฅ ๊ธฐ๋ณธ ํƒ์ƒ‰ ๋ฐฉ๋ฒ•์ธ ์ˆœ์ฐจ ํƒ์ƒ‰์— ๋Œ€ํ•ด์„œ ์‚ด์ง ๋‹ค๋ฃจ๊ณ  ๊ฐ€๋ณด์Ÿˆ ! ์ˆœ์ฐจํƒ์ƒ‰(Sequential Search)์ด๋ž€ ? ๋ฆฌ์ŠคํŠธ ์•ˆ์— ์žˆ๋Š” ํŠน์ •ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์•ž์—์„œ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ• - ๋ฆฌ์ŠคํŠธ์— ํŠน์ • ๊ฐ’์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ์ฒดํฌ - ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒํ˜•์—์„œ ํŠน์ •ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” count() ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€์—์„œ์˜ ์ˆœ์ฐจํƒ์ƒ‰ # ์ˆœ์ฐจํƒ์ƒ‰ ์†Œ์Šค์ฝ”๋“œ def sequential_search(n, target, array): # ๊ฐ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๋ฉฐ for i in range(n): # ํ˜„์žฌ์˜ ์›์†Œ๊ฐ€ ์ฐพ๊ณ ์žํ•˜๋Š” ์›์†Œ์™€ ๋™์ผํ•œ ๊ฒฝ์šฐ if array[i] == target: return i + 1 # ํ˜„์žฌ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜ . ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋‹ˆ๊นŒ 1๋”ํ•˜๊ธฐ ..

Algorithm/CodingTest - Python 2022. 4. 7. 10:36