Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- k=int(input("k= "))
- l= input("a= ")
- l=l.split(" ")
- for i in l:
- i=int(i)
- print(l)
- def quickselect(l, k, pivot_fn=random.choice):
- pivot = pivot_fn(l)
- lows = [el for el in l if el < pivot]
- highs = [el for el in l if el > pivot]
- pivots = [el for el in l if el == pivot]
- if k < len(lows):
- return quickselect(lows, k, pivot_fn)
- elif k < len(lows) + len(pivots):
- return pivots[0]
- else:
- return quickselect(highs, k - len(lows) - len(pivots), pivot_fn)
- def pivot_mediana(a):
- if len(a)<=5:
- return sorted(a)[len(a)//2]
- subliste=[sorted (a[i:i+5])for i in range(0,len(a),5)]
- mediane=[sl[len(sl)//2] for sl in subliste]
- return pivot_mediana(mediane)
- r=quickselect(l,k,pivot_mediana)
- print(r)
Add Comment
Please, Sign In to add comment