YS's develop story

๋ฒ„๋ธ” ์ •๋ ฌ, ์„ ํƒ ์ •๋ ฌ, ์‚ฝ์ž… ์ •๋ ฌ ์ •๋ฆฌ ๋ณธ๋ฌธ

Algorithm

๋ฒ„๋ธ” ์ •๋ ฌ, ์„ ํƒ ์ •๋ ฌ, ์‚ฝ์ž… ์ •๋ ฌ ์ •๋ฆฌ

Yusang 2021. 7. 29. 14:21

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฒ„๋ธ” ์ •๋ ฌ, ์„ ํƒ ์ •๋ ฌ, ์‚ฝ์ž… ์ •๋ ฌ ์ •๋ฆฌ with Python

 

๐Ÿฅ˜ ๋ฒ„๋ธ” ์ •๋ ฌ

๋ฒ„๋ธ” ์ •๋ ฌ์ด๋ž€?

์ธ์ ‘ํ•œ ๋‘ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•ด์„œ ์•ž์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋’ค์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํฌ๋ฉด, ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พธ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

 

Python ์ฝ”๋“œ

def bubbleSort(list):
    swaped = False
    for index in range(len(list) - 1):
        for index2 in range(len(list) - index - 1):
            if list[index2] > list[index2 + 1]:
                list[index2], list[index2 + 1] = list[index2 + 1], list[index2]
                swaped = True
            if swaped == False:
                break
    return list

 

์‹œ๊ฐ„ ๋ณต์žก๋„ 

 

๐Ÿณ ์„ ํƒ ์ •๋ ฌ

์„ ํƒ ์ •๋ ฌ์ด๋ž€?

 

Python ์ฝ”๋“œ

def selectionSort(list):
    for stand in range(len(list) - 1):
        lowest = stand
        for index in range(stand + 1, len(list)):
            if list[lowest] > list[index]:
                lowest = index
        list[lowest], list[stand] = list[stand], list[lowest]
    return list

 

์‹œ๊ฐ„ ๋ณต์žก๋„

 

๐Ÿช ์‚ฝ์ž… ์ •๋ ฌ

์‚ฝ์ž… ์ •๋ ฌ ์ด๋ž€?

  • ์‚ฝ์ž… ์ •๋ ฌ์€ ๋‘ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ์ธ๋ฑ์Šค(key ๊ฐ’) ์•ž์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ(B)๋ถ€ํ„ฐ ๋น„๊ตํ•ด์„œ key ๊ฐ’์ด ๋” ์ž‘์œผ๋ฉด, B๊ฐ’์„ ๋’ค ์ธ๋ฑ์Šค๋กœ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฅผ key ๊ฐ’์ด ๋” ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต, ๊ทธ๋ฆฌ๊ณ  ํฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋‚œ ์œ„์น˜ ๋ฐ”๋กœ ๋’ค์— key ๊ฐ’์„ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

Python ์ฝ”๋“œ

def insertionSort(list):
    for index in range(len(list) - 1):
        for index2 in range(index + 1, 0, -1):
            if list[index2] < list[index2 - 1]:
                list[index2], list[index2 - 1] = list[index2 - 1], list[index2]
            else:
                break
    return list

์‹œ๊ฐ„ ๋ณต์žก๋„

 

Comments