mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
35 lines
693 B
Python
35 lines
693 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def merge_sort(array):
|
|
|
|
# part 1: recursively divide the array into subarrays
|
|
if len(array) < 2:
|
|
return array
|
|
|
|
mid = len(array) // 2
|
|
left = merge_sort(array[:mid])
|
|
right = merge_sort(array[mid:])
|
|
|
|
# part 2: merge the subarrays
|
|
result = []
|
|
i, j = 0, 0
|
|
|
|
while i < len(left) and j < len(right):
|
|
if left[i] <= right[j]:
|
|
result.append(left[i])
|
|
i +=1
|
|
else:
|
|
result.append(right[j])
|
|
j +=1
|
|
|
|
if left[i:]:
|
|
result.extend(left[i:])
|
|
if right[j:]:
|
|
result.extend(right[j:])
|
|
|
|
return result
|
|
|