개발 => 복습 후 재정리 대기/Python

[Python] Srot (Buble, Selection, Insertion)

장 상 현 2021. 5. 28.

거품 정렬

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

선택 정렬

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

삽입 정렬

def insertSort(x):
	for i in range(1, len(x)):
		j = i - 1
		key = x[i]
		while x[j] > key and j >= 0:
			x[j+1]  = x[j]
			j = j - 1
		x[j+1] = key
	return x

댓글