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
์๊ฐ ๋ณต์ก๋