코드

1. 삽입 정렬

N = int(input())
data = []
for _ in range(N):
    a = int(input())
    data.append(a)

for stand in range(len(data)-1):
    lowest = stand
    for i in range(stand+1, len(data)):
        if data[lowest] > data[i]:
            lowest = i
    data[stand], data[lowest] = data[lowest], data[stand]

for i in data:
    print(i)

 

2. 버블 정렬

N = int(input())
data = []
for _ in range(N):
    a = int(input())
    data.append(a)

for i in range(len(data)-1):
    swap = False
    for j in range(len(data)-i-1):
        if data[j] > data[j+1]:
            data[j], data[j+1] = data[j+1], data[j]
            swap = True
    if swap == False:
        break

for i in data:
    print(i)

 

3. 퀵 정렬

def qsort(data):
    if len(data) <= 1:
        return data
    left, right = [], []
    pivot = data[0]
    for i in range(1, len(data)):
        if pivot > data[i]:
            left.append(data[i])
        else:
            right.append(data[i])
    return qsort(left) + [pivot] + qsort(right)

N = int(input())
arr = []
for _ in range(N):
    a = int(input())
    arr.append(a)
ans = qsort(arr)
for i in ans:
    print(i)

 

 

복사했습니다!