Guest User

Untitled

a guest
Feb 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. import random
  2. import time
  3. import numpy as np
  4.  
  5. def partition(A,p,r):
  6. x = A[r-1] #the pivot element
  7. i = p - 1
  8. for j in range(r-p+1):
  9. if A[j] <= x: #the elements should be less or equal to the pivot
  10. i = i + 1
  11. A[i],A[j] = A[j], A[i] #swap the pivots to the right positions of the left part
  12. A[j+1],A[r] = A[r], A[j+1]
  13. return i +1
  14.  
  15. def quicksort(A,p,r):
  16. if p < r:
  17. q = A[r-1]
  18. partition(A,p,r)
  19. quicksort(A, p, q-1) #resursively quicksort the left or right part of previous pivots
  20. quicksort(A, q+1, r)
  21.  
  22. random.seed(123)
  23. lst1 = [random.random() for a in range(100000)]
  24. quicksort(lst1, 0, 99)
Add Comment
Please, Sign In to add comment