ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

์ •๋ ฌ ๋ฐฉ์‹

์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ ฌ ๋ฐฉ์‹์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ๋กœ 2์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” Bubble, Select, Insert ์ •๋ ฌ์ด๋‹ค. ๋‘ ๋ฒˆ์งธ๋Š” ๋ถ„ํ• -์ •๋ณต ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ณ‘ํ•ฉ, ํ€ต ์ •๋ ฌ์ด๋‹ค.

 

2์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ ฌ ๋ฐฉ์‹

ํ•ด๋‹น ๋ฐฉ์‹์€ ์ตœ์•…์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^2)์ด ๋œ๋‹ค.

์ด์™€ ๊ฐ™์€ ์ด์œ ๋Š” 2๊ฐœ์˜ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

Bubble Sort

์„œ๋กœ ์ธ์ ‘ํ•œ ๋‘ ์›์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. swap์ด ๋” ์ด์ƒ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด, ์ค‘๋‹จํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

 

    def bubble_sort(nums):
        for i in range(len(nums)):
            swap = False
            for j in range(1, len(nums)):
                if nums[j - 1] > nums[j]:
                    nums[j - 1], nums[j] = nums[j], nums[j - 1]
                    swap = True
            if not swap:
                break
    

 

Select Sort

์ •๋ ฌ ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’๋“ค ์ด์™ธ์— ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜์ง€ ์•Š๋Š” in-place ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค. ๋ง๊ทธ๋Œ€๋กœ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ถ€ํ„ฐ ์„ ํƒํ•˜๊ณ , ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ํƒ์ƒ‰ํ•˜์—ฌ ๋งจ ์•ž์˜ ์œ„์น˜๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

def select_sort(num):
    for i in range(len(num)):
        min_idx = i
        for j in range(i + 1, len(num)):
            if num[min_idx] > num[j]:
                min_idx = j
        num[i], num[min_idx] = num[min_idx], num[i]

 

Insert Sort

์›์†Œ์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํ•ด๋‹น ์›์†Œ๋ฅผ ์ ์ ˆํ•œ ์œ„์น˜์— ์‚ฝ์ž…ํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. Bubble, Select์™€ ๋‹ฌ๋ฆฌ ๋ผ์šด๋“œ๊ฐ€ ์ง„ํ–‰ํ•  ์ˆ˜๋ก ๋น„๊ต ๋Œ€์ƒ์ด ์ฆ๊ฐ€ํ•œ๋‹ค.

 

def insert_sort(num):
    for i in range(len(num)):
        key = num[i]
        j = i - 1
        while j >= 0 and key < num[j]:
            num[j+1] = num[j]
            j -= 1
            num[j+1] = key

 


 

๋ถ„ํ• -์ •๋ณต์„ ์‚ฌ์šฉํ•˜๋Š” ์ •๋ ฌ ๋ฐฉ์‹

2์ค‘ ๋ฐ˜๋ณต๋ฌธ ๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์ด๋ฉฐ, ํฐ ๋ฌธ์ œ๋ฅผ ์ž‘์€ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ๋‹จ๊ณ„์ ์œผ๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ์‹์ด๋ผ๊ณ  ํ•˜์—ฌ ๋ถ„ํ• -์ •๋ณต์ด๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.

 

Merge Sort

๋ฐฐ์—ด์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด left, right๋ฅผ ๊ฐ๊ฐ ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋˜๋ฉฐ, ๋” ์ด์ƒ ๋‚˜๋ˆ„์–ด์ง€์ง€ ์•Š๊ฒŒ ๋˜๋ฉด ๊ฐ’์ด ๋ฐ˜ํ™˜๋˜์–ด ์ •๋ ฌ๋˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

def merge_sort(num):
    length = len(num)
    if length <= 1:
        return

    mid = length // 2
    left = num[:mid]
    right = num[mid:]
    merge_sort(left)
    merge_sort(right)
    # left index
    i = 0
    # right index
    j = 0
    # num list index
    k = 0
    # left, right์˜ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์›๋ž˜ ๋ฐฐ์—ด์— ๊ฐ’์„ ์ •๋ ฌํ•จ
    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            num[k] = left[i]
            i += 1
        else :
            num[k] = right[j]
            j += 1
        k += 1
	# ๋Œ€์†Œ๋น„๊ต๊ฐ€ ๋˜์ง€ ์•Š์€ ๋‚˜๋จธ์ง€ ๊ฐ’๋“ค์„ ๋„ฃ์Œ
    while i < len(left):
        num[k] = left[i]
        i += 1
        k += 1
    while j < len(right):
        num[k] = right[j]
        j += 1
        k += 1

 

Quick Sort

๊ธฐ์ค€ ๊ฐ’์„ ์ •ํ•˜์—ฌ ์›์†Œ์™€ ๊ธฐ์ค€ ๊ฐ’์„ ๋น„๊ต ํ›„ ๊ฐ’์ด ์ž‘์œผ๋ฉด less๋กœ ํฌ๋ฉด more๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์ •๋ ฌ์„ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

 

def quick_sort(num):
    length = len(num)
    if length <= 1:
        return num

    pivot = num[0]
    less = []
    more = []
    for i in range(1, len(num)):
        if num[i] < pivot:
            less.append(num[i])
        else:
            more.append(num[i])
    return quick_sort(less) + [pivot] + quick_sort(more)
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€