Advertisement
Guest User

ewr

a guest
Nov 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. def median(M, a, b):
  2.     return M[(a + b) // 2]
  3. def first(M, a, b):
  4.     return M[a]
  5. def last(M, a, b):
  6.     return M[b]
  7.  
  8. def __qsort(M, a, b, supf):
  9.     r = 0
  10.     c = supf(M, a, b)
  11.     x, y = a, b
  12.     while x < y:
  13.         r += 3
  14.         while M[x] < c:
  15.             r += 1
  16.             x += 1
  17.         while M[y] > c:
  18.             r += 1
  19.             y -= 1
  20.         if x <= y:    
  21.             M[x], M[y] = M[y], M[x]
  22.             x += 1
  23.             y -= 1
  24.     if y > a:
  25.         r += __qsort(M, a, y, supf)
  26.     if x < b:
  27.         r += __qsort(M, x, b, supf)
  28.     return r
  29.  
  30. def qsort(M, supf):
  31.     return __qsort(M, 0, len(M) - 1, supf)
  32.        
  33. A = list(map(int, input().split()))
  34. print(A)
  35. qsort(A, median)
  36. print(A)
  37. print(qsort(A, median), qsort(A, first), qsort(A, last))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement