Alhiris

Untitled

Jan 9th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import random
  2. k=int(input("k= "))
  3. l= input("a= ")
  4. l=l.split(" ")
  5. for i in l:
  6.     i=int(i)
  7. print(l)
  8.  
  9. def quickselect(l, k, pivot_fn=random.choice):
  10.  
  11.     pivot = pivot_fn(l)
  12.  
  13.     lows = [el for el in l if el < pivot]
  14.     highs = [el for el in l if el > pivot]
  15.     pivots = [el for el in l if el == pivot]
  16.  
  17.     if k < len(lows):
  18.         return quickselect(lows, k, pivot_fn)
  19.     elif k < len(lows) + len(pivots):
  20.         return pivots[0]
  21.     else:
  22.         return quickselect(highs, k - len(lows) - len(pivots), pivot_fn)
  23.  
  24. def pivot_mediana(a):
  25.     if len(a)<=5:
  26.         return sorted(a)[len(a)//2]
  27.     subliste=[sorted (a[i:i+5])for i in range(0,len(a),5)]
  28.     mediane=[sl[len(sl)//2] for sl in subliste]
  29.     return pivot_mediana(mediane)
  30. r=quickselect(l,k,pivot_mediana)
  31. print(r)
Add Comment
Please, Sign In to add comment