[4주차1일차] Chapter 06_알고리즘 알고리즘2

HA_·2023년 10월 26일
0

버블 정렬

버블정렬이란?

처음부터 끝까지 인접하는 인덱스의 값을 순차적으로 비교하면서 큰 숫자를 가장 끝으로 옮기는 알고리즘이다.

nums = [10, 2, 7, 21, 0]
print(f'not sorted nums: {nums}')

length = len(nums) - 1
for i in range(length):
    for j in range(length - i):
        if nums[j] > nums[j+1]:
            # temp = nums[j]
            # nums[j] = nums[j+1]
            # nums[j + 1] = temp
            nums[j] , nums[j+1] = nums[j+1], nums[j]

        print(nums)
    print()
print(f'sorted nums: {nums}')
# 결괏값:
not sorted nums: [10, 2, 7, 21, 0]
[2, 10, 7, 21, 0]
[2, 7, 10, 21, 0]
[2, 7, 10, 21, 0]
[2, 7, 10, 0, 21]

[2, 7, 10, 0, 21]
[2, 7, 10, 0, 21]
[2, 7, 0, 10, 21]

[2, 7, 0, 10, 21]
[2, 0, 7, 10, 21]

[0, 2, 7, 10, 21]

sorted nums: [0, 2, 7, 10, 21]

버블 정렬(실습)

버블정렬

새 학년이 되어 학습에 20명의 새로운 학생들이 모였다. 학생들을 키 순서로 줄 세워보자. 학생들의 키는 random 모듈을 이용해서 170~185 사이로 생성한다.

  • 모듈 생성
import  copy
def bubbleSort(ns, deepCopy=True):

    if deepCopy:
        cns = copy.copy(ns) # students를 그대로 쓰지 않고 새로운 거 하나 생성
    else:
        cns = ns

    length = len(cns) - 1
    for i in range(length):
        for j in range(length -i):
            if cns[j] > cns[j + 1]:
                cns[j], cns[j+1] = cns[j+1], cns[j]

    return cns
  • 실행파일
import random as rd
import sortMod as sm

students = []

for i in range(20):
    students.append(rd.randint(170, 185))

print(f'students: {students}')

# sortedStudents = sm.bubbleSort(students)
sortedStudents = sm.bubbleSort(students, deepCopy=False)


print(f'students: {students}')
print(f'students: {sortedStudents}')
# 결괏값:
students: [183, 174, 172, 184, 172, 175, 177, 172, 184, 185, 170, 170, 173, 172, 178, 183, 179, 176, 173, 172]
students: [170, 170, 172, 172, 172, 172, 172, 173, 173, 174, 175, 176, 177, 178, 179, 183, 183, 184, 184, 185]
students: [170, 170, 172, 172, 172, 172, 172, 173, 173, 174, 175, 176, 177, 178, 179, 183, 183, 184, 184, 185]

삽입정렬

삽입정렬이란?

정렬되어 있는 자료 배열과 비교해서, 정렬 위치를 찾는다.

삽입정렬(실습)

실습

1부터 100까지의 난수 100개를 생성하고, 다음의 요구 사항을 만족하는 모듈을 만들어보자.

import random
import sortMod as sm

nums = random.sample(range(1, 1000), 100)
print(f'not sorted numbers: {nums}')

# 객체 생성
sn = sm.SortNumbers(nums)

# 오름차순(ascending)
sn.setSort()
sortedNumbers = sn.getSortedNumbers()
print(f'sortedNumbers by ASC: {sortedNumbers}')

# 내림차순(ascending)
sn.isAscending(False)
sn.setSort()
sortedNumbers = sn.getSortedNumbers()
print(f'sortedNumbers by DESC: {sortedNumbers}')

# min & max
print(f'min: {sn.getMinNumber()}')
print(f'max: {sn.getMaxNumber()}')
# 결괏값:
not sorted numbers: [141, 946, 918, 326, 324, 368, 307, 597, 314, 710, 831, 219, 329, 834, 88, 365, 89, 585, 899, 192, 835, 864, 743, 220, 931, 516, 640, 863, 15, 578, 143, 790, 439, 611, 744, 246, 951, 610, 528, 428, 732, 442, 330, 860, 889, 510, 412, 960, 914, 205, 277, 748, 474, 525, 146, 653, 969, 547, 718, 784, 350, 740, 816, 196, 409, 928, 351, 883, 808, 781, 147, 903, 336, 95, 600, 810, 756, 760, 97, 109, 768, 898, 457, 978, 370, 337, 857, 467, 668, 39, 828, 132, 709, 730, 961, 482, 982, 63, 9, 836]
sortedNumbers by ASC: [9, 15, 39, 63, 88, 89, 95, 97, 109, 132, 141, 143, 146, 147, 192, 196, 205, 219, 220, 246, 277, 307, 314, 324, 326, 329, 330, 336, 337, 350, 351, 365, 368, 370, 409, 412, 428, 439, 442, 457, 467, 474, 482, 510, 516, 525, 528, 547, 578, 585, 597, 600, 610, 611, 640, 653, 668, 709, 710, 718, 730, 732, 740, 743, 744, 748, 756, 760, 768, 781, 784, 790, 808, 810, 816, 828, 831, 834, 835, 836, 857, 860, 863, 864, 883, 889, 898, 899, 903, 914, 918, 928, 931, 946, 951, 960, 961, 969, 978, 982]
sortedNumbers by DESC: [982, 978, 969, 961, 960, 951, 946, 931, 928, 918, 914, 903, 899, 898, 889, 883, 864, 863, 860, 857, 836, 835, 834, 831, 828, 816, 810, 808, 790, 784, 781, 768, 760, 756, 748, 744, 743, 740, 732, 730, 718, 710, 709, 668, 653, 640, 611, 610, 600, 597, 585, 578, 547, 528, 525, 516, 510, 482, 474, 467, 457, 442, 439, 428, 412, 409, 370, 368, 365, 351, 350, 337, 336, 330, 329, 326, 324, 314, 307, 277, 246, 220, 219, 205, 196, 192, 147, 146, 143, 141, 132, 109, 97, 95, 89, 88, 63, 39, 15, 9]
min: 9
max: 982

0개의 댓글