← 목차로 돌아가기 / Back to Index

🧪 Ch11. 정렬 (기본) — 실습 코드 / Lab Code: Sorting (Basic)

자료구조와 알고리즘 실습 | Data Structures & Algorithms Lab

💡 학습 팁: 코드를 직접 타이핑하면서 익히세요! 숨겨진 코드는 버튼을 눌러 확인하기 전에 먼저 스스로 생각해 보세요.
💡 Study Tip: Type the code yourself to learn! Try to think about hidden sections before revealing them.

📘 교재 예제 (Code)

📘 Textbook Examples (Code)

Code11-01.py

배열에서 최솟값의 위치를 찾습니다

def findMinIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMinIdx(testAry)
print('최솟값 -->', testAry[minPos])

Code11-01.py

Finds the index of the minimum value in an array

def findMinIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMinIdx(testAry)
print('Minimum -->', testAry[minPos])

Code11-02.py

최솟값 위치를 이용해 두 값을 교환합니다

## 함수 선언 부분 ##
def findMinIdx(ary) :
## 전역 변수 선언 부분 ##
before = [188, 162, 168, 120, 50, 150, 177, 105]
after = []

## 메인 코드 부분 ##
Code11-02.py

Swaps two values using the minimum index

## Function Declarations ##
def findMinIdx(ary) :
## Global Variables ##
before = [188, 162, 168, 120, 50, 150, 177, 105]
after = []

## Main Code ##
print('Before sorting -->', before)
for _ in range(len(before)) :
	minPos = findMinIdx(before)
	after.append(before[minPos])
	del(before[minPos])
print('After sorting -->', after)
Code11-03.py

선택 정렬 알고리즘을 구현합니다

## 함수 선언 부분 ##
def selectionSort(ary) :
## 전역 변수 선언 부분 ##
dataAry = [188, 162, 168, 120, 50, 150, 177, 105]

## 메인 코드 부분 ##
Code11-03.py

Implements the selection sort algorithm

## Function Declarations ##
def selectionSort(ary) :
## Global Variables ##
dataAry = [188, 162, 168, 120, 50, 150, 177, 105]

## Main Code ##
print('Before sorting -->', dataAry)
dataAry = selectionSort(dataAry)
print('After sorting -->', dataAry)
Code11-04.py

삽입할 위치를 찾는 함수를 구현합니다

def findInsertIdx(ary, data) :
testAry = []
insPos = findInsertIdx(testAry, 55)
print('삽입할 위치 -->' , insPos)

testAry = [33, 77, 88]
insPos = findInsertIdx(testAry, 55)
print('삽입할 위치 -->' , insPos)

testAry = [33, 55, 77, 88]
insPos = findInsertIdx(testAry, 100)
print('삽입할 위치 -->' , insPos)
Code11-04.py

Implements a function to find the insertion position

def findInsertIdx(ary, data) :
testAry = []
insPos = findInsertIdx(testAry, 55)
print('Insert position -->' , insPos)

testAry = [33, 77, 88]
insPos = findInsertIdx(testAry, 55)
print('Insert position -->' , insPos)

testAry = [33, 55, 77, 88]
insPos = findInsertIdx(testAry, 100)
print('Insert position -->' , insPos)
Code11-05.py

삽입 정렬의 이동 과정을 확인합니다

## 함수 선언 부분 ##
def findInsertIdx(ary, data) :
## 전역 변수 선언 부분 ##
before = [188, 162, 168, 120, 50, 150, 177, 105]
after = []

## 메인 코드 부분 ##
Code11-05.py

Demonstrates the shifting process of insertion sort

## Function Declarations ##
def findInsertIdx(ary, data) :
## Global Variables ##
before = [188, 162, 168, 120, 50, 150, 177, 105]
after = []

## Main Code ##
print('Before sorting -->', before)
for i in range(len(before)) :
	data = before[i]
	insPos = findInsertIdx(after, data)
	after.insert(insPos, data)
print('After sorting -->', after)
Code11-06.py

삽입 정렬 알고리즘을 구현합니다

## 함수 선언 부분 ##
def insertionSort(ary) :
## 전역 변수 선언 부분 ##
dataAry = [188, 162, 168, 120, 50, 150, 177, 105]

## 메인 코드 부분 ##
Code11-06.py

Implements the insertion sort algorithm

## Function Declarations ##
def insertionSort(ary) :
## Global Variables ##
dataAry = [188, 162, 168, 120, 50, 150, 177, 105]

## Main Code ##
print('Before sorting -->', dataAry)
dataAry = insertionSort(dataAry)
print('After sorting -->', dataAry)
Code11-07.py

선택 정렬과 삽입 정렬의 성능을 비교합니다

## 함수 선언 부분 ##
def selectionSort(ary) :
## 전역 변수 선언 부분 ##
moneyAry = [7, 5, 11, 6, 9, 80000, 10, 6, 15, 12]

## 메인 코드 부분 ##
Code11-07.py

Compares performance of selection sort and insertion sort

## Function Declarations ##
def selectionSort(ary) :
## Global Variables ##
moneyAry = [7, 5, 11, 6, 9, 80000, 10, 6, 15, 12]

## Main Code ##
print('Allowance Before sorting -->', moneyAry)
moneyAry = selectionSort(moneyAry)
print('Allowance After sorting -->', moneyAry)
print('Allowance median --> ', moneyAry[len(moneyAry)//2])
Code11-08.py

파일 목록을 정렬하는 프로그램입니다

import os

## 함수 선언 부분 ##
def makeFileList(folderName) :
def insertionSort(ary) :
## 전역 변수 선언 부분 ##
fileAry = []

## 메인 코드 부분 ##
Code11-08.py

Sorts a list of files using sorting algorithms

import os

## Function Declarations ##
def makeFileList(folderName) :
def insertionSort(ary) :
## Global Variables ##
fileAry = []

## Main Code ##
fileAry = makeFileList('C:/Program Files/Common Files')
fileAry = insertionSort(fileAry)
print('Sort filenames in reverse -->', fileAry)

📗 응용 예제 (Ex)

📗 Applied Examples (Ex)

EX11-02.py

정렬 응용: 선택 정렬 심화 실습

## 함수 선언 부분 ##
def selectionSort(ary) :
## 전역 변수 선언 부분 ##
ary2 = [[55, 33, 250, 44],
		 [88,  1,  67, 23],
		 [199,222, 38, 47],
		 [155,145, 20, 99]]
ary1 = []

## 메인 코드 부분 ##
EX11-02.py

Sorting application: advanced selection sort practice

## Function Declarations ##
def selectionSort(ary) :
## Global Variables ##
ary2 = [[55, 33, 250, 44],
		 [88,  1,  67, 23],
		 [199,222, 38, 47],
		 [155,145, 20, 99]]
ary1 = []

## Main Code ##
for i in range(len(ary2)) :
	for k in range(len(ary2[i])) :
		ary1.append(ary2[i][k])

print('After 1D conversion, before sorting -->', ary1)
ary1 = selectionSort(ary1)
print('After 1D conversion, after sorting -->', ary1)
print('Median --> ', ary1[len(ary1)//2])
Ex11-01.py

정렬 응용: 점수 기반 삽입 정렬

## 함수 선언 부분 ##
def scoreSort(ary) :
## 전역 변수 선언 부분 ##
scoreAry = [['선미', 88], ['초아', 99], ['화사', 71], ['영탁', 78], ['영웅', 67], ['민호', 92]]

## 메인 코드 부분 ##
Ex11-01.py

Sorting application: insertion sort by score

## Function Declarations ##
def scoreSort(ary) :
## Global Variables ##
scoreAry = [['Sunmi', 88], ['Choa', 99], ['Hwasa', 71], ['YoungTak', 78], ['Hero', 67], ['Minho', 92]]

## Main Code ##
print('Before sorting -->', scoreAry)
scoreAry = scoreSort(scoreAry)
print('After sorting -->', scoreAry)

print('## Team assignments by grade ##')
for i in range(len(scoreAry)//2) :
	print(scoreAry[i][0], ':' , scoreAry[len(scoreAry)-1-i][0])

📙 자가 실습 (Self)

📙 Self Practice (Self)

Self11-01.py

최댓값 찾기 함수를 직접 구현합니다

def findMaxIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMaxIdx(testAry)
print('최댓값 -->', testAry[minPos])
Self11-01.py

Implements a find-maximum function yourself

def findMaxIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMaxIdx(testAry)
print('Maximum -->', testAry[minPos])
Self11-02.py

삽입 정렬을 직접 구현합니다

import  random
## 함수 선언부
def findInsertIdx(ary, data) :
## 전역 변수부
before = []
after = []

## 메인 코드부
Self11-02.py

Implements insertion sort yourself

import  random
## Function Declarations
def findInsertIdx(ary, data) :
## Global Variables
before = []
after = []

## Main Code
before = [random.randint(0,200) for _ in range(10)]
print('Before sorting -->', before)
for i in range(len(before)) :
    data = before[i]
    insPos = findInsertIdx(after, data)
    after.insert(insPos, data)
print('After sorting -->', after)