자료구조와 알고리즘 실습 | Data Structures & Algorithms Lab
배열에서 최솟값의 위치를 찾습니다
def findMinIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMinIdx(testAry)
print('최솟값 -->', testAry[minPos])
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])
최솟값 위치를 이용해 두 값을 교환합니다
## 함수 선언 부분 ## def findMinIdx(ary) :
## 전역 변수 선언 부분 ## before = [188, 162, 168, 120, 50, 150, 177, 105] after = [] ## 메인 코드 부분 ##
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)
선택 정렬 알고리즘을 구현합니다
## 함수 선언 부분 ## def selectionSort(ary) :
## 전역 변수 선언 부분 ## dataAry = [188, 162, 168, 120, 50, 150, 177, 105] ## 메인 코드 부분 ##
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)
삽입할 위치를 찾는 함수를 구현합니다
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)
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)
삽입 정렬의 이동 과정을 확인합니다
## 함수 선언 부분 ## def findInsertIdx(ary, data) :
## 전역 변수 선언 부분 ## before = [188, 162, 168, 120, 50, 150, 177, 105] after = [] ## 메인 코드 부분 ##
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)
삽입 정렬 알고리즘을 구현합니다
## 함수 선언 부분 ## def insertionSort(ary) :
## 전역 변수 선언 부분 ## dataAry = [188, 162, 168, 120, 50, 150, 177, 105] ## 메인 코드 부분 ##
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)
선택 정렬과 삽입 정렬의 성능을 비교합니다
## 함수 선언 부분 ## def selectionSort(ary) :
## 전역 변수 선언 부분 ## moneyAry = [7, 5, 11, 6, 9, 80000, 10, 6, 15, 12] ## 메인 코드 부분 ##
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])
파일 목록을 정렬하는 프로그램입니다
import os ## 함수 선언 부분 ## def makeFileList(folderName) :
def insertionSort(ary) :
## 전역 변수 선언 부분 ## fileAry = [] ## 메인 코드 부분 ##
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)
정렬 응용: 선택 정렬 심화 실습
## 함수 선언 부분 ## def selectionSort(ary) :
## 전역 변수 선언 부분 ## ary2 = [[55, 33, 250, 44], [88, 1, 67, 23], [199,222, 38, 47], [155,145, 20, 99]] ary1 = [] ## 메인 코드 부분 ##
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])
정렬 응용: 점수 기반 삽입 정렬
## 함수 선언 부분 ## def scoreSort(ary) :
## 전역 변수 선언 부분 ## scoreAry = [['선미', 88], ['초아', 99], ['화사', 71], ['영탁', 78], ['영웅', 67], ['민호', 92]] ## 메인 코드 부분 ##
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])
최댓값 찾기 함수를 직접 구현합니다
def findMaxIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMaxIdx(testAry)
print('최댓값 -->', testAry[minPos])Implements a find-maximum function yourself
def findMaxIdx(ary) :
testAry = [55, 88, 33, 77]
minPos = findMaxIdx(testAry)
print('Maximum -->', testAry[minPos])삽입 정렬을 직접 구현합니다
import random ## 함수 선언부 def findInsertIdx(ary, data) :
## 전역 변수부 before = [] after = [] ## 메인 코드부
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)