Update README.md

This commit is contained in:
marina 2023-08-02 13:40:47 -07:00 committed by GitHub
parent f7779dc6e3
commit b53b78285d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -245,3 +245,30 @@ class MaxHeap:
- using the heap to sort the input array
* heapsort traditionally uses a max-heap to sort the array, although a min-heap also works.
* this is not a stable sort.
<br>
----
### top k elements problem (`O(klog(N) + N)`)
<br>
1. construct a max (min) heap and add all elements into it (`O(N)`)
2. traverse and delete the top element, storing the value into a resulting array
3. repeat 2. until all k elements are removed (`O(k * log(N))`)
<br>
----
### the kth-element problem
<br>
1. construct a max (min) heap and add all elements into it (`O(N)`)
2. traverse and delete the top element
3. repeat 2. until k-th largest (smallest) is found (`O(k * log(N))`)
<br>