Advertisement
Guest User

quick_sort_in_place

a guest
Mar 5th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. import sys
  2. # Enter your code here. Read input from STDIN. Print output to STDOUT
  3. #def helpquicksort(ar):
  4.     #print quicksort(ar)
  5.     #print quicksort(ar[
  6. def partition(ar,position):
  7.     #ar[0:len(ary)]=ary[0:len(ar)]
  8.     middle=[ar[position]]
  9.     right,left=ar[0:position],ar[position+1:]
  10.     #print ' '.join(map(str,right+middle+left))
  11.     join(right,middle,left)
  12.     quicksort(right)
  13.     join(right,middle,left)
  14.     quicksort(left)
  15.     join(right,middle,left)
  16.    
  17. def join(right,middle,left):
  18.     ar= right+middle+left
  19.    
  20.    
  21.        
  22.    
  23.        
  24. def quicksort(ar):
  25.     if len(ar) == 1:
  26.         return ar
  27.     pivot=ar[-1]
  28.     count = -1
  29.     position=0
  30.     for j in range(len(ar)):
  31.         if ar[j] <= pivot:
  32.             temp = ar[j]
  33.             i=0
  34.             while True:
  35.                 if i < len(ar):
  36.                     if ar[i] >= ar[j] and i != count:
  37.                         ar[j]=ar[i]
  38.                         ar[i]=temp
  39.                         count+=1
  40.                         position=i
  41.                         #print ar
  42.                         break;
  43.                 else:
  44.                     break;
  45.                 i+=1
  46.     #return ar          
  47.     #print ar
  48.     partition(ar,position)
  49.     #return ar
  50.    
  51.                    
  52.            
  53. if __name__ == '__main__':
  54.     n = int(sys.stdin.readline())
  55.     ar = list(map(int, sys.stdin.readline().split()))
  56.     quicksort(ar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement