data structures - Finding K smallest values in an array (Heap vs QuickSelect) -
let's assume have array , wish find k smallest values of :
there 2 approaches :
1.using quick select algorithm (o(n) time complexity , o(1) space)
2.using min heap data structure (o(nlogk) time complexity , o(k) space)
i'd know when 1 preferred on one.
i guess both of them can distributed.
check out:-
quick select sorting or heap
as sorting whole dataset quite slow, makes sense select top k items , sort few ‘top’ elements giving impression user whole dataset sorted pages through result set. give running time of o(k*log(k) + n) opposed o(n*log(n)) faster if k reasonably small (few hundreds example).
an other approach work heap , keep popping smallest number while putting larger receiving n numbers stream. work o(n*log(k)) running time heap holds k elements height log(k) while test n numbers in total, although it’s expected running time larger quick select , sort combination.
Comments
Post a Comment