Algorithm3 [알고리즘] 삽입 정렬(insertion sort) # insertion sort def insertion_sort(data): for idx1 in range(len(data) - 1): # 처음에 range(idx1 + 1, len(data) - 1)로 적었다. for idx2 in range(idx1 + 1, 0, -1): if data[idx2] < data[idx2 - 1]: # 이 부분은 swap이 아니라 data[idx2] = data[idx2 - 1]로 적었다. data[idx2], data[idx2 - 1] = data[idx2 - 1], data[idx2] else: break return data import random data = random.sample(range(100), 10) print(insertion_sort(data)).. 2021. 6. 21. [알고리즘] 선택 정렬(selection sort) # selection sort def selection_sort(data): for stand in range(len(data) - 1): lowest = stand for idx2 in range(stand + 1, len(data)): if data[idx2] < data[lowest]: lowest = idx2 data[stand], data[lowest] = data[lowest], data[stand] return data import random data = random.sample(range(100), 10) print(selection_sort(data)) 2021. 6. 21. [알고리즘] 버블 정렬(bubble sort) # bubble sort def bubble_sort(data): for idx1 in range(len(data) - 1): swap = False for idx2 in range(len(data) - 1 - idx1): if data[idx2] > data[idx2 + 1]: data[idx2], data[idx2 + 1] = data[idx2 + 1], data[idx2] swap = True if swap == False: break return data import random data = random.sample(range(100), 15) print(bubble_sort(data)) 2021. 6. 21. 이전 1 다음