Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- Dynamic Programming
- 머신러닝
- dns
- Django
- Algorithm
- 기본 정렬
- IPV4
- network
- 알고리즘
- Python
- 자료구조
- Windows Server
- ICQA
- 파이썬
- Machine learning
- 실기
- 네트워크 자격증
- deep learning
- 딥러닝
- 네트워크
- 네트워크 관리사
- 네트워크 관리사 2급
- 자격증
- 패스트캠퍼스
- 프로토콜
- Protocol
- 코딩테스트
- FTP
- 밑바닥부터 시작하는 딥러닝
- 서브넷마스크
Archives
- Today
- Total
쳉지로그
[알고리즘 이론] 병합 정렬 (Merge Sort) 본문
병합 정렬(Merge Sort)
- 재귀 용법을 활요한 정렬 알고리즘
- 리스트를 반으로 나눠 비슷한 크기의 두 부분 리스트로 분할
- 각 부분 리스트를 재귀적으로 합병 정렬을 이용해 정렬
- 두 부분 리스트를 다시 하나의 정렬된 리스트로 합병
""" 병합 정렬 구현 코드 """
def merge(left, right):
merged_list = list()
l_point, r_point = 0, 0
# Case 1. left, right 둘 다 있는 경우
while len(left) > l_point and len(right) > r_point:
if left[l_point] > right[r_point]:
merged_list.append(right[r_point])
r_point += 1
else:
merged_list.append(left[l_point])
l_point += 1
# Case 2. left 데이터가 없는 경우
while len(left) > l_point:
merged_list.append(left[l_point])
l_point += 1
# Case 3. right 데이터가 없는 경우
while len(right) > r_point:
merged_list.append(right[r_point])
r_point += 1
return merged_list
def merge_split(data):
if len(data) <= 1:
return data
medium = int(len(data)/2)
left = merge_split(data[:medium])
right = merge_split(data[medium:])
return merge(left, right)
import random
data_list = random.sample(range(100), 10) # 랜덤으로 10개 데이터 추출해서 리스트 생성
merge_split(data_list) # 병합 정렬
- 시뮬레이션 사이트
VisuAlgo - Sorting (Bubble, Selection, Insertion, Merge, Quick, Counting, Radix)
VisuAlgo is free of charge for Computer Science community on earth. If you like VisuAlgo, the only payment that we ask of you is for you to tell the existence of VisuAlgo to other Computer Science students/instructors that you know =) via Facebook, Twitter
visualgo.net
'코딩테스트 > 알고리즘 이론' 카테고리의 다른 글
[알고리즘 이론] 동적 계획법(Dynamic Programming) (0) | 2021.12.08 |
---|---|
[알고리즘 이론] 재귀 용법 (Recursive Call, 재귀 호출) (0) | 2021.12.07 |
[알고리즘 이론] 선택 정렬 (Selection Sort) (0) | 2021.12.07 |
[알고리즘 이론] 삽입 정렬 (Insertion Sort) (0) | 2021.12.06 |
[알고리즘 이론] 버블 정렬 (Bubble Sort) (0) | 2021.12.06 |
Comments