GCD & LCM(์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ & ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜)
ยท
๐Ÿ“š Computer Science/Algorithms
์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ def GCD(num1, num2) : if num2 == 0: return num1 else : return GCD(num2, num1 % num2) ์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜(LCM) LCM = (a * b) / GCD(a, b) ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ ๊ตฌํ•˜๊ธฐ ์˜ˆ์ œ a, b = map(int, input().split()) def GCD(num1, num2) : if num2 == 0: return num1 else : return GCD(num2, num1 % num2) print(GCD(a, b)) print((a * b) // GCD(a,b))
Cocktail Sort(์นตํ…Œ์ผ ์ •๋ ฌ)
ยท
๐Ÿ“š Computer Science/Algorithms
์–‘๋ฐฉํ–ฅ ๊ฑฐํ’ˆ์ •๋ ฌ (bidirectional bubble sort)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋ฉฐ ๋ฒ„๋ธ”์ •๋ ฌ์˜ ๋ณ€ํ˜•์ด๋‹ค. ํ•œ๋ฒˆ์˜ ๋ฃจํ‹ด๋งˆ๋‹ค ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟ” ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ๋ฒ„๋ธ”์ •๋ ฌ๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€๋Š” ์•Š์ง€๋งŒ ๋ฒ„๋ธ” ์ •๋ ฌ๋ณด๋‹ค ๋น ๋ฅด๋‹ค. ํŒŒ์ด์ฌ ์˜ˆ์ œ ์ฝ”๋“œ def cocktail(arr, a, b): swapped = True while swapped == True: swapped = False for i in range(a, b): if arr[i] > arr[i+1]: arr[i], arr[i+1] = arr[i+1], arr[i] if swapped == False: break swapped = False b = b - 1 for i in range(b-1, a-1, -1): if arr[i] > arr[i+1]: arr[i], ..
Bubble Sort(๋ฒ„๋ธ” ์ •๋ ฌ)
ยท
๐Ÿ“š Computer Science/Algorithms
Bubble Sort(๋ฒ„๋ธ” ์ •๋ ฌ)์€ ๋‘ ์ธ์ ‘ํ•œ ์›์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n^2) ์œผ๋กœ ์ƒ๋‹นํžˆ ๋Š๋ฆฌ์ง€๋งŒ, ์ฝ”๋“œ์ž์ฒด๊ฐ€ ๋‹จ์ˆœํ•ด์„œ ์ž์ฃผ ์‚ฌ์šฉ๋œ๋‹ค. ์ด๋•Œ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์ •๋ ฌ์„ ํ•˜๊ฒŒ๋˜๋ฉด ์นตํ…Œ์ผ ์ •๋ ฌ์ด ๋œ๋‹ค. ํŒŒ์ด์ฌ ์˜ˆ์ œ ์ฝ”๋“œ def bubbleSort(x): length = len(x)-1 for i in range(length): for j in range(length-i): if x[j] > x[j+1]: x[j], x[j+1] = x[j+1], x[j] return x
Binary Search (์ด์ง„ํƒ์ƒ‰)
ยท
๐Ÿ“š Computer Science/Algorithms
์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฒ”์œ„๋ฅผ ๋ฐ˜์”ฉ ๋‚˜๋ˆ„์–ด ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ ์†๋„๊ฐ€ ๋น ๋ฆ„ ์ƒํ™ฉ ์†๋„ ์ตœ์  O(1) ๋ณดํ†ต O(logn) ์ตœ์•… O(logn) ๋™์ž‘ ๋ฐฐ์—ด์˜ ์ค‘๊ฐ„๊ฐ’ ์„ค์ • ์ค‘๊ฐ„๊ฐ’๊ณผ ๊ฒ€์ƒ‰๊ฐ’ ๋น„๊ต ๊ฒ€์ƒ‰๊ฐ’๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ - ํƒ์ƒ‰์™„๋ฃŒ ๊ฒ€์ƒ‰๊ฐ’์ด ์ค‘๊ฐ„๊ฐ’๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ - low๋ฅผ ์ค‘๊ฐ„๊ฐ’+1 ๋กœ ์กฐ์ • ๊ฒ€์ƒ‰๊ฐ’์ด ์ค‘๊ฐ„๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ - high๋ฅผ ์ค‘๊ฐ„๊ฐ’-1 ๋กœ ์กฐ์ • low๊ฐ€ high๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€๊ฒฝ์šฐ๊นŒ์ง€ ๋ฐ˜๋ณต(๊ฐ’์„ ์ฐพ์€ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด) ์ด์ง„ํƒ์ƒ‰ ๊ตฌํ˜„ def binarySearch(data, num): low = 0 high = len(data) - 1 while(low data[mid]: low = mid + 1 else : high = mid - 1 return False ์ด์ง„ํƒ์ƒ‰ ์‚ฌ์šฉ ์˜ˆ์‹œ ์ฝ”๋“œ n..