Sort

iznueยท2023๋…„ 12์›” 22์ผ
0

์ฝ”๋”ฉํ…Œ์ŠคํŠธ

๋ชฉ๋ก ๋ณด๊ธฐ
3/8
post-thumbnail

๐Ÿ“— Sort

  • O(nยฒ)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„
    - ๋ฒ„๋ธ” ์ •๋ ฌ(Bubble Sort)
    - ์„ ํƒ ์ •๋ ฌ(Selection Sort)
    - ์‚ฝ์ž… ์ •๋ ฌ(Insertion Sort)
  • O(n log n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„
    - ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)
    - ํ€ต ์ •๋ ฌ(Quick Sort)

๐Ÿ”” ๋ฒ„๋ธ” ์ •๋ ฌ(Bubble Sort)

  • ์ธ์ ‘ํ•œ ๋‘ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์•ž ๋˜๋Š” ๋’ค์—์„œ๋ถ€ํ„ฐ ๋ฐ˜๋ณตํ•˜์—ฌ ์ž‘์€ ์ˆ˜๋ถ€ํ„ฐ ํฐ ์ˆ˜๋กœ ์ •๋ ฌ ๊ฐ€๋Šฅ
arr = [9,8,7,6,5,4,3,2,1]
def bubble_sort(arr):
	n = len(arr)
    for i in range(n-1):
    	for j in range(n-i-1):
        	if arr[j] > arr[j+1]:
            	arr[j], arr[j+1] = arr[j+1], arr[j]

๐Ÿ”” ์„ ํƒ ์ •๋ ฌ(Selection Sort)

  • ํ•œ ๋ฐ”ํ€ด ๋Œ๋•Œ๋งˆ๋‹ค ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ๋งจ ์•ž๊ณผ ๊ตํ™˜ํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์•ž์—์„œ๋ถ€ํ„ฐ ์ •๋ ฌํ•œ๋‹ค๋Š” ํŠน์„ฑ์ด ์žˆ์Œ
arr = [8,4,6,2,9,1,3,7,5]
def selection_sort(arr):
	n = len(arr)
    for i in range(n):
    	min_index = i
        for j in range(i+1, n):
        	if arr[j] < arr[min_index]:
            	min_index = j
        arr[i], arr[min_index] = arr[min_index], arr[i]

๐Ÿ”” ์‚ฝ์ž… ์ •๋ ฌ(Insertion Sort)

  • ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃน์„ ๋Š˜๋ฆฌ๋ฉฐ ์ถ”๊ฐ€๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์•Œ๋งž์€ ์ž๋ฆฌ์— ์‚ฝ์ž…ํ•˜๋ฉฐ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹
arr = [8,4,6,2,9,1,3,7,5]
def insertions_sort(arr):
	n = len(arr)
    for i in range(1, n):
    	for j in range(i, 0, -1):
        	if arr[j-1] > arr[j]:
            	arr[j-1], arr[j] = arr[j], arr[j-1]

๐Ÿ”” ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)

  • ๋ถ„ํ•  ์ •๋ณต๊ณผ ์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„๊ต์  ๋‚ฎ์€ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ ธ ์•ž์˜ ๋ฐฉ์‹๋“ค๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹์Œ
  • ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ณ  ๋‹ค์‹œ ํ•ฉ์น˜๋Š” ๊ณผ์ •์—์„œ ๊ทธ๋ฃน์œผ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋˜๋ฉฐ 2n๊ฐœ์˜ ๊ณต๊ฐ„์ด ํ•„์š”ํ•จ
arr = [8,4,6,2,9,1,3,7,5]
def merge_sort(arr):
	if len(arr) < 2:
    	return arr
    mid = len(arr) // 2
    low_arr = merge_sort(arr[:mid])
    high_arr = merge_sort(arr[mid:])
    merge_arr = []
    l, h = 0, 0
    while l < len(low_arr) and h < len(high_arr):
    	if low_arr[l] < high_arr[h]:
        	merge_arr.append(low_arr[l])
            l += 1
        else:
        	merge_arr.append(high_arr[h])
            h += 1
    merge_arr += low_arr[l:]
    merge_arr += high_arr[h:]
    return merge_arr

๐Ÿ”” ํ€ต ์ •๋ ฌ(Quick Sort)

  • ๋ณ‘ํ•ฉ ์ •๋ ฌ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ถ„ํ•  ์ •๋ณต ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋น„๊ต์  ๋น ๋ฅธ ์†๋„๋กœ ์ˆ˜ํ–‰ํ•จ
  • ์ถ”๊ฐ€์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ํ•„์š”์—†์Œ
  • pivot์„ ์„ค์ •ํ•˜๊ณ  ๊ทธ ๊ธฐ์ค€์— ๋”ฐ๋ผ ๋ถ„ํ• ํ•จ, ๋ณ‘ํ•ฉ ์ •๋ ฌ์€ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„ํ• ํ•œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ์Œ
arr = [8,4,6,2,9,1,3,7,5]
def quick_sort(arr):
	if len(arr) < 1:
    	return arr
    pivot = len(arr) // 2
    f_arr, p_arr, b_arr = [], [], []
    for value in arr:
    	if value < arr[pivot]:
        	f_arr.append(value)
        elif value > arr[pivot]:
        	b_arr.append(value)
        else:
        	p_arr.append(value)
    return quick_sort(f_arr) + quick_sort(p_arr) + quick_sort(b_arr)

๐Ÿ”” sort() VS sorted()

  • sort() : list.sort()
    • ์ž…๋ ฅ๊ฐ’์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋งŒ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
    • ๋ฆฌ์ŠคํŠธ์˜ ์›๋ณธ๊ฐ’์„ ์ง์ ‘ ์ˆ˜์ •ํ•จ
    • ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋”ฐ๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ
  • sorted() : sorted(list, key)
    • ๋ฆฌ์ŠคํŠธ ์›๋ณธ ๊ฐ’์€ ์œ ์ง€ํ•จ
    • ์–ด๋–ค ์ž…๋ ฅ๊ฐ’์ด๋“  ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ
    • ์ •๋ ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ ํ›„์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•จ
  • sorted() ํ•จ์ˆ˜๋Š” Timsort ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•จ

๐Ÿ“˜ ๊ด€๋ จ ๋ฌธ์ œ

โญ K๋ฒˆ์งธ์ˆ˜
โญ ๊ฐ€์žฅ ํฐ ์ˆ˜
โญ H-Index

profile
โ‚Šหš โŠน โ™ก https://github.com/iznue

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