Skip to content

Instantly share code, notes, and snippets.

@huaxlin
Created January 7, 2023 16:07
Show Gist options
  • Save huaxlin/9bbb5bf10b9c5a21035f784cd91d7822 to your computer and use it in GitHub Desktop.
Save huaxlin/9bbb5bf10b9c5a21035f784cd91d7822 to your computer and use it in GitHub Desktop.
def mergesort(seq):
mid = len(seq) // 2
lft, rgt = seq[:mid], seq[mid:]
if len(lft) > 1: lft = mergesort(lft)
if len(rgt) > 1: rgt = mergesort(rgt)
res = []
while lft and rgt:
if lft[-1] >= rgt[-1]: res.append(lft.pop())
else: res.append(rgt.pop())
return (lft or rgt) + res[::-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment