Last active
August 29, 2015 14:01
-
-
Save jepio/fdeea7da10d4aaaf5726 to your computer and use it in GitHub Desktop.
A 'quick' implementation of the recursive quicksort.
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
""" Two python implementations of the quicksort algorithm.""" | |
__all__ = ["sort_np", "sort_pure"] | |
import numpy as np | |
def sort_np(array): | |
""" For numpy arrays, with masking. """ | |
if len(array) <= 1: | |
return array | |
pivot = array[0] | |
less_than = array[array < pivot] | |
equal = array[array == pivot] | |
more_than = array[array > pivot] | |
less_than = sort_np(less_than) | |
more_than = sort_np(more_than) | |
array = np.append(less_than, equal) | |
array = np.append(array, more_than) | |
return array | |
def sort_pure(array): | |
""" Using builtin list comprehension. """ | |
if len(array) < 1: | |
return array | |
pivot = array[0] | |
less = [number for number in array if number < pivot] | |
more = [number for number in array if number > pivot] | |
equal = [number for number in array if number == pivot] | |
array = sort_pure(less) + equal + sort_pure(more) | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment