๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ทจ์—…์„ ์œ„ํ•œ python....(ใ…œใ…œ)/python_study

[์•Œ๊ณ ๋ฆฌ์ฆ˜/์ž๋ฃŒ๊ตฌ์กฐ] ์„ฑ๊ณต์ ์ธ..์–ด์ฉŒ๊ตฌ

by ์ž„๋ฆฌ๋‘ฅ์ ˆ 2022. 8. 4.
๋ฐ˜์‘ํ˜•

https://inf.run/o3Vf

[๋ฌด๋ฃŒ] ์„ฑ๊ณต์ ์ธ ์ฝ”๋”ฉ ์ธํ„ฐ๋ทฐ๋ฅผ ์œ„ํ•œ ์ฝ”๋”ฉ ์ธํ„ฐ๋ทฐ ์ •๋ณตํ•˜๊ธฐ - ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

์„ฑ๊ณต์ ์œผ๋กœ ์ฝ”๋”ฉ ์ธํ„ฐ๋ทฐ (๊ฐœ๋ฐœ์ž ๋ฉด์ ‘)๋ฅผ ๋ณด๊ธฐ ์œ„ํ•ด ๊ฐ€์ ธ์•ผ ํ•˜๋Š” ํ•„์ˆ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฌธ์ œ์™€ ํ•ด๋ฒ•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค., - ๊ฐ•์˜ ์†Œ๊ฐœ | ์ธํ”„๋Ÿฐ...

www.inflearn.com


ํœ˜๊ฐˆ๊ธฐ๋ฉฐ ํ•„๊ธฐํ•ด์„œ ๋‚ด ๋”ด์— ํฌ์ธํŠธ๋ผ๊ณ  ๋‚จ๊ธด... ๋‹ค์†Œ ์ฃผ๊ด€... ๋งŽ์ด ์ฃผ๊ด€์ ์ธ ์ •๋ฆฌ


Section 0 ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ (OOP) ๊ฐœ๋…

  • Class

object's data fields, methods. ๋ญ ํ•จ์ˆ˜๋‚˜ ๋ฐ์ดํ„ฐ ์ •์˜ํ•˜๋Š” ๊ณณ

  • Object

class's instance ์œ„ 'fields'์— ์ •์˜ํ•  ๊ฒƒ

  • Encapsulation
  • Inheritance

๋ง ๊ทธ๋Œ€๋กœ ์ƒ์†. ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ์œ„ํ•ด์„œ ex) Tesla extends car

  • Polymorphism

Overloading: ํด๋ž˜์Šค ์•ˆ ๊ฐ™์€ ์ด๋ฆ„์ธ๋ฐ ๋‹ค๋ฅด๊ฒŒ ์“ฐ์ผ ๋•Œ,
Overriding: ๊ฐ™์€ ํ•จ์ˆ˜์ธ๋”” ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ ์“ฐ์ผ ๋•Œ
์„ ๋งํ•˜์…จ๋‹ค ๊ทผ๋ฐ ์ž๋ฐ” ๋‹ค ๊นŒ๋จน์Œ ใ…Žใ…Ž;

  • Abstraction

์ด๊ฒŒ ์‚ด์ง ์ดํ•ดํ•œ ๊ฒŒ ๋งž๋Š” ์ง€ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜
( ๊ฐ€์ƒํด๋ž˜์Šค() ) ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ€์ƒ์˜ concept? ํ‹€์„ ์žก์•„์ฃผ์–ด์•ผ
๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ์ฝ”๋“œ ๊ณต์œ ์‹œ ๊ณ ์œ ํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค !
ex) car(start() stop()) -> Tesla (start() stop())


Section 1 Big-O notation ๋น…์˜คํ‘œ๊ธฐ๋ฒ•
๋ถ„๋ช… ์ด๊ฒƒ๋„ ํ•™๊ต์—์„œ ๋ฐฐ์› ๋Š”๋ฐ ๊ธฐ์–ต์ด ์•ˆ๋‚˜๋ฒ„๋ฆฌ๋Š” ๋งˆ๋ฒ•..
์ฝ”๋“œ์˜ ํšจ์œจ์„ฑ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ performance๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

* nO(1) = O(1) ๋‹ค์Œ๊ณผ ๊ฐ™์€ n์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํฌ๊ฒŒ ์˜ํ–ฅ์ฃผ์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์ฐธ๊ณ 
  1. O(1) : order 1 : ํ•˜๋‚˜์— ์ ‘๊ทผ. ex) push, pop, hash
  2. O(log n) : ex) Binary Search Tree Access, Search, Insertion, Deletion ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ.. ์œผ์œฝ .. n=8, log 8 = log2^3 => 3
  3. O(n) : ex) traverse(์ˆœํšŒ) tree, linked list ๋ฆฌ์ŠคํŠธ ๊ฐ™์€ ๊ฑฐ ์ „์ฒด ๋‹ค ๋ˆ๋‹ค !
  4. O(n log n) : ex) Quick sort, Merge sort, Heap sort
  5. O(n^2) : ํ•จ์ˆ˜์•ˆ์— ํ•จ์ˆ˜. ex) Insertion sort, Bubble sort, Selection sort

Section 2 Sort

sort๋Š” ์•„๋ฌด๋ž˜๋„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ดํ•ดํ•˜๋Š” ๊ฒŒ ํŽธํ•œ ๋“ฏํ•˜๋‹ค, ํ˜น์€ ๊ทธ๋ฆผ?
https://github.com/minsuk-heo/problemsolving/tree/master/sort

GitHub - minsuk-heo/problemsolving

Contribute to minsuk-heo/problemsolving development by creating an account on GitHub.

github.com

https://gmlwjd9405.github.io/2018/05/06/algorithm-selection-sort.html

  • Bubble Sort

performance - O(n^2) / space complexity O(n)
์ธ์ ‘ item swap์œผ๋กœ sort.
์„œ๋กœ ์ธ์ ‘ํ•œ ๋‘ ์›์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
์ธ์ ‘ํ•œ 2๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋น„๊ตํ•˜์—ฌ ํฌ๊ธฐ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์„œ๋กœ ๊ตํ™˜ํ•œ๋‹ค.
์„ ํƒ ์ •๋ ฌ๊ณผ ๊ธฐ๋ณธ ๊ฐœ๋…์ด ์œ ์‚ฌํ•˜๋‹ค.

import unittest

def bubblesort(alist):
    for i in range(len(alist)-1):
        for j in range(len(alist)-1):
            if alist[j] > alist[j+1]:
                alist[j], alist[j+1] = alist[j+1], alist[j]
    return alist


class unit_test(unittest.TestCase):
    def test(self):
        self.assertEqual([1, 2, 3, 4, 5, 6], bubblesort([4, 6, 1, 3, 5, 2]))
        self.assertEqual([1, 2, 3, 4, 5, 6], bubblesort([6, 4, 3, 1, 2, 5]))
        self.assertEqual([1, 2, 3, 4, 5, 6], bubblesort([6, 5, 4, 3, 2, 1]))
  • Selection Sort

O(n^2) / minimum value ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ž๋ฆฌ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•˜๋ฉด์„œ swaping
์ œ์ž๋ฆฌ ์ •๋ ฌ(in-place sorting) ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ•˜๋‚˜
์ฒซ ๋ฒˆ์งธ ์ˆœ์„œ์—๋Š” ์ฒซ ๋ฒˆ์งธ ์œ„์น˜์— ๊ฐ€์žฅ ์ตœ์†Ÿ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
๋‘ ๋ฒˆ์งธ ์ˆœ์„œ์—๋Š” ๋‘ ๋ฒˆ์งธ ์œ„์น˜์— ๋‚จ์€ ๊ฐ’ ์ค‘์—์„œ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

import unittest

def selectionSort(input):
    for i in range(len(input) - 1):
        # assume the min is the first element
        idx_min = i
        j = i + 1

        # test against elements after i to find the smallest
        while j < len(input):
            if(input[j] < input[idx_min]):
                # found new minimum; remember its index
                idx_min = j
            j = j + 1

        if idx_min is not i:
            # swap
            input[idx_min], input[i] = input[i], input[idx_min]

    return input

class unit_test(unittest.TestCase):
    def test(self):
        self.assertEqual([1, 2, 3, 4, 5, 6], selectionSort([4, 6, 1, 3, 5, 2]))
        self.assertEqual([1, 2, 3, 4, 5, 6], selectionSort([6, 4, 3, 1, 2, 5]))
        self.assertEqual([1, 2, 3, 4, 5, 6], selectionSort([6, 5, 4, 3, 2, 1]))

  • Insertion Sort

๊ฐ„๋‹จํ•œ๋ฐ ํšจ์œจ์„ฑ์€ ๋–จ์–ด์ง€๋Š”.. ๋น„๊ตํ•˜๊ณ  ์˜†๋ณด๋‹ค ๋˜ ์ž‘์œผ๋ฉด swap... swap...

์‚ฝ์ž… ์ •๋ ฌ์€ ๋‘ ๋ฒˆ์งธ ์ž๋ฃŒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๊ทธ ์•ž(์™ผ์ชฝ)์˜ ์ž๋ฃŒ๋“ค๊ณผ ๋น„๊ตํ•˜์—ฌ ์‚ฝ์ž…ํ•  ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ ํ›„ ์ž๋ฃŒ๋ฅผ ๋’ค๋กœ ์˜ฎ๊ธฐ๊ณ  ์ง€์ •ํ•œ ์ž๋ฆฌ์— ์ž๋ฃŒ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.
์ฆ‰, ๋‘ ๋ฒˆ์งธ ์ž๋ฃŒ๋Š” ์ฒซ ๋ฒˆ์งธ ์ž๋ฃŒ, ์„ธ ๋ฒˆ์งธ ์ž๋ฃŒ๋Š” ๋‘ ๋ฒˆ์งธ์™€ ์ฒซ ๋ฒˆ์งธ ์ž๋ฃŒ, ๋„ค ๋ฒˆ์งธ ์ž๋ฃŒ๋Š” ์„ธ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ์ฒซ ๋ฒˆ์งธ ์ž๋ฃŒ์™€ ๋น„๊ตํ•œ ํ›„ ์ž๋ฃŒ๊ฐ€ ์‚ฝ์ž…๋  ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค. ์ž๋ฃŒ๊ฐ€ ์‚ฝ์ž…๋  ์œ„์น˜๋ฅผ ์ฐพ์•˜๋‹ค๋ฉด ๊ทธ ์œ„์น˜์— ์ž๋ฃŒ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์œ„ํ•ด ์ž๋ฃŒ๋ฅผ ํ•œ ์นธ์”ฉ ๋’ค๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
์ฒ˜์Œ Key ๊ฐ’์€ ๋‘ ๋ฒˆ์งธ ์ž๋ฃŒ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

import unittest

def insertion_sort(input):

    for idx, valueToInsert in enumerate(input):
        # select the hole position where number is to be inserted
        holePosition = idx

        # check if previous no. is larger than value to be inserted
        while holePosition > 0 and input[holePosition-1] > valueToInsert:
            input[holePosition - 1], input[holePosition] = input[holePosition], input[holePosition-1]
            holePosition = holePosition - 1

    return input

class unit_test(unittest.TestCase):
    def test(self):
        self.assertEqual([1, 2, 3, 4, 5, 6], insertion_sort([4, 6, 1, 3, 5, 2]))
        self.assertEqual([1, 2, 3, 4, 5, 6], insertion_sort([6, 4, 3, 1, 2, 5]))
        self.assertEqual([1, 2, 3, 4, 5, 6], insertion_sort([6, 5, 4, 3, 2, 1]))

  • Merge Sort

O(nlogn) / O(n) 8->4,4->2,2->1,1 ์™„์ „ ์ชผ๊ฐœ์„œ ์ •๋ ฌํ•˜๊ณ  ํ•ฉ์ณํ•ฉ์ณ
์ด๊ฑด ๋ง๋ณด๋‹ค ๊ทธ๋ฆผ์ด ์ดํ•ดํ•˜๊ธฐ ํŽธํ•œ

https://gmlwjd9405.github.io/2018/05/08/algorithm-merge-sort.html
__author__ = 'Minsuk Heo'

def mergeSort(alist):
    print("Splitting ",alist)
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0
        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i < len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
    print("Merging ",alist)

alist = [6,2,4,1,3,7,5,8]
mergeSort(alist)
print(alist)

  • Quick Sort

O(nlogn) (worst : O(n^2)) / O(n) ํšจ์œจ๋ฉด์—์„œ good
Pivot(๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์„ ์‚ฌ์šฉ)์„ ์ง€์ •ํ•ด์„œ ๋น„๊ตํ•ด๊ฐ„๋‹ค. ์ž‘์œผ๋ฉด ์™ผ์œผ๋กœ ์ด๋™! ํฌ๋ฉด ์šฐ๋กœ ์ด๋™! ๋๋‚ฌ์œผ๋ฉด pivot ๋‹ค์‹œ ์ง€์ •!

import unittest

def quick_sort(list, start, end):
    # repeat until sublist has one item
    # because the algorithm is using in-place space, we can not use len(list) instead we use start, end for sublist
    if start < end:
        # get pivot using partition method
        pivot = partition(list, start, end)
        # recurse quick sort left side from pivot
        quick_sort(list, start, pivot-1)
        # recurse quick sort right side from pivot
        quick_sort(list,pivot+1, end)
    return list

def partition(list, start, end):
    # use end item as initial pivot
    pivot = end
    # use start as initial wall index
    wall = start
    left = start
    # repeat until left item hit the end of list
    while left < pivot:
        # if left item is smaller than pivot, swap left item with wall and move wall to right
        # this will ensure items smaller than pivot stay left side from the wall and
        # the items greater than pivot stay right side from the wall
        if list[left] < list[pivot]:
            list[wall], list[left] = list[left], list[wall]
            wall = wall + 1
        left = left + 1
    # when left hit the end of list, swap pivot with wall
    list[wall], list[pivot] = list[pivot], list[wall]
    # now left side of wall are the items smaller than wall
    # now right side of pivot are the items greater than wall
    # wall is the new pivot
    pivot = wall
    return pivot

class unit_test(unittest.TestCase):
    def test(self):
        list = [8, 13, 2, 6, 1, 4]
        self.assertEqual([1, 2, 4, 6, 8, 13], quick_sort(list,0,len(list)-1))
        list = [8, 1, 2, 5, 10, 14, 7, 21]
        self.assertEqual([1, 2, 5, 7, 8, 10, 14, 21], quick_sort(list, 0, len(list) - 1))

๋ฐ˜์‘ํ˜•

์ตœ๊ทผ๋Œ“๊ธ€

์ตœ๊ทผ๊ธ€

skin by ยฉ 2024 ttutta