Skip to content

Instantly share code, notes, and snippets.

@buptxge
Created March 22, 2016 07:27
Show Gist options
  • Save buptxge/2f203c702ceba5c65b9b to your computer and use it in GitHub Desktop.
Save buptxge/2f203c702ceba5c65b9b to your computer and use it in GitHub Desktop.
QuickSort python example.
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