๐Ÿ”ฅ TIL - Day 34

Kim Dae Hyunยท2021๋…„ 10์›” 21์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
40/93

๋ช‡ ๋ฒˆ์„ ํ•ด๋„ ๋งค๋ฒˆ ์ดˆ๋ฉด๊ฐ™์€ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜... ์–ธ์  ๊ฐ„ ์ต์ˆ™ํ•ด์ง€๊ฒ ์ง€..

์ตœ๊ทผ ๋ช‡ ๊ฐœ์˜ ๋ฉด์ ‘์ˆ˜๊ธฐ์—์„œ ๋ž˜ํผ๋Ÿฐ์Šค ์—†์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฌป๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ดค๋‹ค. ๋‚˜๋Š” ๋ญ๋ผ๊ณ  ๋Œ€๋‹ตํ• ๊นŒ? ๋ฒ„๋ธ”, ์„ ํƒ, ์‚ฝ์ž… ์ •๋„..?

์ดˆ๋ผํ•˜๋‹ค... ๋‹ค์‹œ ์•Œ์•„๋ณด๊ณ  ๋” .. ๋” ์•Œ์•„๋ณด์ž..


๐Ÿ“Œ ์‚ฝ์ž…์ •๋ ฌ

์‚ฝ์ž…์ •๋ ฌ์€ ์•ž์œผ๋กœ ๋‚˜์•„๊ฐ€๋ฉด์„œ ์ ์  ์ •๋ ฌ ํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์ด๋‹ค.

์ฒซ๋ฒˆ์งธ ๋ฐ˜๋ณต์—์„œ ๋‘๋ฒˆ์งธ ์š”์†Œ์˜ ์ž๋ฆฌ๋ฅผ ์žก๋Š”๋‹ค.
์ฒซ๋ฒˆ์งธ์™€ ๋‘๋ฒˆ์งธ๋ฅผ ๋น„๊ต!

๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต์—์„œ ์„ธ๋ฒˆ์งธ ์š”์†Œ์˜ ์ž๋ฆฌ๋ฅผ ์žก๋Š”๋‹ค.
์„ธ๋ฒˆ์งธ๋ถ€ํ„ฐ ๋‘๋ฒˆ์งธ ์ฒซ๋ฒˆ์งธ ์ญ‰ ๋น„๊ตํ•˜๋ฉฐ ์™ผ์ชฝ์˜ ๊ฐ’์ด ๋” ํด ๋•Œ๊นŒ์ง€ ์ด๋™ํ•˜๋ฉด์„œ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พผ๋‹ค.

์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด n๋ฒˆ ๋ฐ˜๋ณต๋งˆ๋‹ค n+1๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„๋Š” ๋ฒ„๋ธ”์ •๋ ฌ๊ณผ ๋™์ผํ•œ O(N) ์ด์ง€๋งŒ ์ž๋ฆฌ๋ฅผ ์žก์•˜์„ ๋•Œ break ๋ฌธ์œผ๋กœ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„๋ธ”์ •๋ ฌ ๋ณด๋‹ค๋Š” ์ข‹์€ ์„ฑ๋Šฅ์„ ๊ฐ–๋Š”๋‹ค.

def insertion_sort(arr):
    n = len(arr)
    for i in range(1, n):
        min_idx = i
        for j in range(i):
            if arr[i-j] < arr[i-j-1]:
                arr[i-j], arr[i-j-1] = arr[i-j-1], arr[i-j]
            else:
                break

๐Ÿ“Œ ๋ณ‘ํ•ฉ์ •๋ ฌ

๋ณ‘ํ•ฉ์ •๋ ฌ์€ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋‰œ๋‹ค.
๋ถ„ํ• ํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ณ‘ํ•ฉ ํ•˜๋Š” ๋‘ ๋ถ€๋ถ„์ด๋‹ค.

๋จผ์ € ๋ถ„ํ• ํ•˜๋Š” ๋ถ€๋ถ„์„ ์‚ดํŽด๋ณด์ž.
์ •๋ ฌํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๊ณ  ๊ณ„์† ์ ˆ๋ฐ˜์”ฉ ์ž˜๋ผ๊ฐ€๋ฉฐ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ์–ป์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์€ ๊ธธ์ด๊ฐ€ 1์ธ ๋ฐฐ์—ด์ด๋‹ค.

๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋ถ„ํ•  ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

def devide(arr):
    n = len(arr)
    if n <= 1:
        return arr
    mid = n // 2
    left_arr = devide(arr[mid:])
    right_arr = devide(arr[:mid])

์ด์ œ ๋ถ„ํ• ๋œ ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•  ์ฐจ๋ก€์ด๋‹ค.
๋ณ‘ํ•ฉํ•˜๋Š” ๋ถ€๋ถ„์„ ์‚ดํŽด๋ณด์ž.

๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋Š” ๋ถ„ํ• ๋œ ์™ผ์ชฝ๋ถ€๋ถ„๊ณผ ์˜ค๋ฅธ์ชฝ๋ถ€๋ถ„ ๋ฐฐ์—ด์„ ๋ฐ›๋Š”๋‹ค.
๊ฐ ๋ฐฐ์—ด์€ ๋…๋ฆฝ์ ์ธ ์ธ๋ฑ์Šค(left_idx, right_idx)๋ฅผ ๊ฐ–๊ณ  ์„œ๋กœ ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ๋” ์ž‘์€ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ณ‘ํ•ฉํ•œ๋‹ค (์˜ค๋ฆ„์ฐจ์ˆœ)

def merge(left, right):
    merge_list = []
    left_idx = 0
    right_idx = 0
    while left_idx < len(left) and right_idx < len(right):
        if left[left_idx] < right[right_idx]:
            merge_list.append(left[left_idx])
            left_idx += 1
        else:
            merge_list.append(right[right_idx])
            right_idx += 1

    if left_idx < len(left):
        for i in range(left_idx, len(left)):
            merge_list.append(left[i])
    else:
        for i in range(right_idx, len(right)):
            merge_list.append(right[i])
    return merge_list

์ „์ฒด์ฝ”๋“œ

def devide(arr):
    n = len(arr)
    if n <= 1:
        return arr
    mid = n // 2
    left_arr = devide(arr[mid:])
    right_arr = devide(arr[:mid])

    return merge(left_arr, right_arr)

def merge(left, right):
    merge_list = []
    left_idx = 0
    right_idx = 0
    while left_idx < len(left) and right_idx < len(right):
        if left[left_idx] < right[right_idx]:
            merge_list.append(left[left_idx])
            left_idx += 1
        else:
            merge_list.append(right[right_idx])
            right_idx += 1

    if left_idx < len(left):
        for i in range(left_idx, len(left)):
            merge_list.append(left[i])
    else:
        for i in range(right_idx, len(right)):
            merge_list.append(right[i])
    return merge_list

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€