Last active
December 16, 2015 02:59
-
-
Save yasuharu519/5366582 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
# coding: utf-8 | |
import random | |
def quick_sort(inlist): | |
if len(inlist) <= 1: | |
return inlist | |
middle = len(inlist) / 2 | |
pivot = inlist.pop(middle) | |
left, right = [], [] | |
for i in inlist: | |
if i <= pivot: | |
left.append(i) | |
else: | |
right.append(i) | |
left = quick_sort(left) | |
right = quick_sort(right) | |
return left + [pivot] + right | |
def isSort(inlist): | |
for i in xrange(len(inlist) - 1): | |
if inlist[i] > inlist[i+1]: | |
return False | |
return True | |
if __name__ == "__main__": | |
testlist = range(100) | |
random.shuffle(testlist) | |
assert isSort(testlist) == False | |
after = quick_sort(testlist) | |
assert isSort(after) == True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment