Alhiris

Untitled

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