버블정렬
# 옆에 있는 값 끼리 계속 비교하면서
# 큰 값을 뒤로 보낸다 (오름차순)
def bubble_sort(ary):
# 마지막은 마지막전이랑 비교 하니 빼준다.
ary_max = len(ary) - 1
# 정렬 해야 할 횟수만큼 반복
for i in range(ary_max):
# 버블정렬 후 i 만큼 뺀다, 뒤에는 정렬이 된 값이 있기때문
# 첫 정렬할때 4번, 그 다음은 3번.. 진행
cnt = ary_max - i
for y in range(cnt):
# 제일 큰 값을 뒤로 보낸다.
# y, y+1이면은 밑에 처럼 작동된다.
# 0 1
# 1 2
# 2 3
# 3 4
if ary[y] > ary[y + 1]:
ary[y], ary[y + 1] = ary[y + 1], ary[y]
return ary
print(bubble_sort([5, 4, 3, 2, 1]))
선택정렬
# 제일 최솟값을 찾아서 위치를 변경 해준다.
def selection_sort(arr):
max_arr = len(arr)
# 배열의 전체를 조회
for i in range(max_arr):
# 최솟값 변수
min_index = i
# y는 이미 정렬 된 수를 제외 후 조회를 한다.
for y in range(i + 1, max_arr):
# 변수에 제일 값이 낮은 인덱스를 저장
if arr[min_index] > arr[y]:
min_index = y
# 초기에 지정한 값이랑 다를경우, 변경사항이 있다면
if min_index != i:
# 스왑
arr[min_index], arr[i] = arr[i], arr[min_index]
return arr
print(selection_sort([5, 4, 3, 2, 1]))
삽입정렬
삽입 정렬 리스트 https://leetcode.com/problems/insertion-sort-list
Insertion Sort List - LeetCode
Can you solve this real interview question? Insertion Sort List - Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head. The steps of the insertion sort algorithm: 1. Insertion sort iterates, consumin
leetcode.com
가장 큰 수 https://leetcode.com/problems/largest-number
Largest Number - LeetCode
Can you solve this real interview question? Largest Number - Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, so you need to return a string instead of an int
leetcode.com
전화번호 목록 https://www.acmicpc.net/problem/5052
5052번: 전화번호 목록
첫째 줄에 테스트 케이스의 개수 t가 주어진다. (1 ≤ t ≤ 50) 각 테스트 케이스의 첫째 줄에는 전화번호의 수 n이 주어진다. (1 ≤ n ≤ 10000) 다음 n개의 줄에는 목록에 포함되어 있는 전화번호가
www.acmicpc.net
'항해99' 카테고리의 다른 글
항해99 알고리즘 13회차 (1) | 2023.12.26 |
---|---|
항해99 알고리즘 12회차 (0) | 2023.12.26 |
항해99 알고리즘 10회차 (1) | 2023.12.22 |
항해99 알고리즘 9회차 (0) | 2023.12.21 |
항해99 알고리즘 8회차 (0) | 2023.12.19 |