Advertisement
Guest User

0, 1, 2 sort

a guest
Nov 27th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1.  
  2. input = [0,1,2,2,2,0,1, 0,2,1,0,0,2,2]
  3. #input = [1,0,1]
  4. N = len(input)
  5.  
  6. print input
  7.  
  8. z=0
  9. t=N-1
  10.  
  11. c=0
  12. i=0
  13. while i != t+1:
  14.   c+=1
  15.   if input[i] == 0:
  16.     if input[z]==0:
  17.       i+=1
  18.     else:
  19.       input[i]=input[z]
  20.       input[z]=0
  21.       i+=1
  22.     z+=1
  23.   elif input[i] == 2:
  24.     if input[t]  == 2:
  25.       pass
  26.     else:
  27.       input[i]=input[t]
  28.       input[t]=2
  29.     t-=1
  30.   else:
  31.     i+=1
  32.  
  33. print input
  34. print N, c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement