all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
from heapq import merge
|
||||
|
||||
def merge_sort(m):
|
||||
if len(m) <= 1:
|
||||
return m
|
||||
|
||||
middle = len(m) / 2
|
||||
left = m[:middle]
|
||||
right = m[middle:]
|
||||
|
||||
left = merge_sort(left)
|
||||
right = merge_sort(right)
|
||||
return list(merge(left, right))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def merge(left, right):
|
||||
result = []
|
||||
left_idx, right_idx = 0, 0
|
||||
while left_idx < len(left) and right_idx < len(right):
|
||||
# change the direction of this comparison to change the direction of the sort
|
||||
if left[left_idx] <= right[right_idx]:
|
||||
result.append(left[left_idx])
|
||||
left_idx += 1
|
||||
else:
|
||||
result.append(right[right_idx])
|
||||
right_idx += 1
|
||||
|
||||
if left:
|
||||
result.extend(left[left_idx:])
|
||||
if right:
|
||||
result.extend(right[right_idx:])
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue