Created
January 7, 2023 16:07
-
-
Save huaxlin/9bbb5bf10b9c5a21035f784cd91d7822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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