출처: [위키피디아]
def split(li):
if len(li) <= 1:
return li
mid_idx = len(li)//2
left_list = split(li[:mid_idx])
right_list = split(li[mid_idx:])
return merge(left_list, right_list)
def merge(left_list, right_list):
total = []
lp = 0
rp = 0
for _ in range(len(left_list) + len(right_list)):
if lp == len(left_list):
total.append(right_list[rp])
rp += 1
elif rp == len(left_list):
total.append(left_list[lp])
lp += 1
elif left_list[lp] > right_list[rp]:
total.append(right_list[rp])
rp += 1
elif left_list[lp] < right_list[rp]:
total.append(left_list[lp])
lp += 1
return total
print(split([3,2,5,4,1,6,9]))
[1, 2, 3, 4, 5, 6, 9]
def merge(left_list, right_list):
merged = list()
left_point, right_point = 0, 0
#왼쪽 오른쪽 둘다 있을 때
while len(left_list) > left_point and len(right_list) > right_point:
if left_list[left_point] < right_list[right_point]:
merged.append(left_list[left_point])
left_point += 1
else:
merged.append(right_list[right_point])
right_point += 1
#왼쪽 데이터만 있을 때 >> 넣기
while len(left_list) > left_point:
merged.append(left_list[left_point])
left_point += 1
#오른쪽 데이터만 남았을 때 >> 넣기
while len(right_list) > right_point:
merged.append(right_list[right_point])
right_point += 1
return merged
def mergesplit(data):
if len(data) <= 1:
return data
mid_point = len(data)//2
left_list = mergesplit(data[:mid_point])
right_list = mergesplit(data[mid_point:])
return merge(left_list,right_list)