Advertisement
michalkowalczyk

QuickSort with Lomuto-Partition

Dec 2nd, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Dec  2 23:09:58 2018
  4.  
  5. @author: micha
  6. """
  7.  
  8. A=[]
  9. from random import randint    
  10. for i in range(0,100):
  11.     A.append(randint(1,100))
  12.  
  13. print(A)
  14. print()
  15.    
  16. def QuickSort(A,p,k):
  17.     if(p<k):
  18.         r = Partition(A,p,k)
  19.         QuickSort(A,p,r-1)
  20.         QuickSort(A,r+1,k)
  21.  
  22. def Partition(A,p,k):
  23.     x=A[k]
  24.     i=p-1
  25.    
  26.     for j in range(p,k):
  27.         if(A[j]<=x):
  28.             i+=1
  29.             A[i],A[j] = A[j],A[i]
  30.     A[i+1],A[k] = A[k],A[i+1]
  31.     return i+1
  32.  
  33.  
  34.    
  35. QuickSort(A,0,len(A)-1)  
  36. print(A)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement