Created
February 17, 2014 12:14
-
-
Save OzTamir/9049523 to your computer and use it in GitHub Desktop.
All sorts of sorts, sort of...
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
from heapq import merge | |
import random | |
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)) | |
def quick_sort(m): | |
if len(m) <= 1: | |
return m | |
pivot = random.choice(m) | |
smaller = [i for i in m if i < pivot] | |
equal = [i for i in m if i == pivot] | |
bigger = [i for i in m if i > pivot] | |
return quick_sort(smaller) + equal + quick_sort(bigger) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment