Created
August 5, 2013 04:34
-
-
Save MatthewReinbold/6153532 to your computer and use it in GitHub Desktop.
#Quick Sort in Python The third in a series of *sorting algorithms* developed in **python**, inspired by the visualization here: http://www.youtube.com/watch?v=kPRA0W1kECg . Quick sort is also know as a partition-exchange sort. ##Pros: - Despite the worst case making O(n^2) comparisons, usual performance is in the O( n log n) range. - Because it…
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
# setup; build a list with random elements | |
import random | |
lNums = [] | |
for item in range(0,10): | |
lNums.append(random.randint(0,1000)) | |
print lNums | |
# quicksort using python's list comprehensions | |
def quicksort( tempList ): | |
if tempList == []: | |
return [] | |
else: | |
# select and remove pivot element from list | |
# there are several options for how to do this | |
pivotValue = tempList[0] | |
lesserList = quicksort( [x for x in tempList[1:] if x < pivotValue]) | |
greaterList = quicksort( [x for x in tempList[1:] if x >= pivotValue]) | |
return lesserList + [pivotValue] + greaterList | |
print quicksort( lNums ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment