Created
June 2, 2013 17:19
-
-
Save 17twenty/5694203 to your computer and use it in GitHub Desktop.
Quicksort Quick Implementation
This file contains hidden or 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
| #!/usr/bin/env python | |
| """Simple implementation of a quicksort""" | |
| def quicksort(array): | |
| if len(array) <= 1: | |
| return array | |
| pivot = array.pop(len(array) / 2) # Naive? | |
| print "Chose pivot ", (pivot) | |
| les = [] | |
| greater = [] | |
| for value in array: | |
| if value <= pivot: | |
| les.append(value) | |
| else: | |
| greater.append(value) | |
| return quicksort(les) + [pivot] + quicksort(greater) | |
| foo = [1, 7, 4, 5, 9, 14, 2, 2, 3] | |
| print quicksort(foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment