#timestamp 2025-03-25

heaps: k-smallest elements

asymptotic running time

add() -> for n elements (n>k):

Θ(k⋅log⁡(k)⏟insertion+(n−k)⋅log⁡(k)⏟deletion)=Θ(nlog⁡k)

get() -> for n elements (n>k):

O(k⋅1+k⋅log⁡(k)+klog⁡(k))=O(k+2klog⁡(k))=O(klog⁡(k))

-> overall time

O(nlog⁡k)

asymptotic memory consumption

-> total memory consumption:

Θ(k)
  1. https://en.cppreference.com/w/cpp/algorithm/sort ↩︎