Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优先队列及堆排序 #131

Open
hunterhug opened this issue Jul 26, 2021 · 3 comments
Open

优先队列及堆排序 #131

hunterhug opened this issue Jul 26, 2021 · 3 comments

Comments

@hunterhug
Copy link
Owner

https://hunterhug.github.io/goa.c/#/algorithm/heaps

@lxc0916
Copy link

lxc0916 commented Aug 20, 2021

第 k 层的非叶子节点的数量为 n/2^k,每一个非叶子节点下沉的最大次数为其子孙的层数:k,而树的层数为 logn 层,那么总的翻转次数计算如下:

因为如下的公式是成立的:

所以翻转的次数计算结果为:2n 次。也就是构建堆的时间复杂度为:O(n)。

这个地方的公式和计算过程没有填上哦 @hunterhug

@hunterhug
Copy link
Owner Author

@lxc0916
第 k 层的非叶子节点的数量为 n/2^k,每一个非叶子节点下沉的最大次数为其子孙的层数:k,而树的层数为 logn 层,那么总的翻转次数计算如下:

因为如下的公式是成立的:

所以翻转的次数计算结果为:2n 次。也就是构建堆的时间复杂度为:O(n)。

这个地方的公式和计算过程没有填上哦 @hunterhug

要看下高等数学,无穷级数部分

@hunterhug
Copy link
Owner Author

我利用最小堆实现了一个有过期时间的内存缓存,https://github.com/hunterhug/gocache/blob/master/README_ZH.md

代码可以借鉴。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants