Last active
December 16, 2015 03:08
-
-
Save shotarok/5367231 to your computer and use it in GitHub Desktop.
Quick sort where space order is O(1)
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, first, last): | |
if last - first < 1: | |
return inlist | |
pivot = inlist[first] | |
left, right = first, last | |
while left != right: | |
if inlist[right] >= pivot: | |
right -= 1 | |
elif inlist[left] < pivot: | |
left += 1 | |
else: | |
inlist[right], inlist[left] = inlist[left], inlist[right] | |
# right equals left | |
middle = right | |
inlist = quick_sort(inlist, first, middle) | |
inlist = quick_sort(inlist, middle+1, last) | |
return inlist | |
なっちゃいそうだね。
if inlist[right] >= pivot:
right -= 1
elif inlist[left] < pivot:
left += 1
ってだけで、うまくいくと思う
update しました。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
これ、pivotにとった数が複数あると無限ループになりそうですね。