Created
March 22, 2016 07:27
-
-
Save buptxge/2f203c702ceba5c65b9b to your computer and use it in GitHub Desktop.
QuickSort python example.
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
import random | |
def sort(a,low,high): | |
if low>=high: | |
return | |
i = low | |
j = high | |
mid_value = a[i] | |
while(True): | |
while (a[i]>=mid_value): | |
i+=1 | |
if i==high: | |
break | |
while (a[j]<=mid_value): | |
j-=1 | |
if j==low: | |
break | |
if (i>=j): | |
break | |
a[i],a[j] = a[j],a[i] | |
a[low],a[j] = a[j],a[low] | |
sort(a,low,j-1) | |
sort(a,j+1,high) | |
def main(a): | |
random.shuffle(a) | |
sort(a,0,len(a)-1) | |
if __name__ == "__main__": | |
a = [] | |
for i in range(1000): | |
a.append(random.randint(-1000,1000)) | |
main(a) | |
print a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment