mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
Update README.md
This commit is contained in:
parent
f7779dc6e3
commit
b53b78285d
1 changed files with 27 additions and 0 deletions
|
@ -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>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue