-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
源码中关于复杂度的错误 #14
Comments
同样疑惑。 |
同样的疑惑,过来确认一下我没错 |
sds sdstrim(sds s, const char *cset)
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我在看redis中的跳跃表的实现,觉得注释中对于复杂度的记录是有问题的。
` *
T = O(N)
*/
int zslRandomLevel(void) {
int level = 1;
while ((random() & 0xFFFF) < (ZSKIPLIST_P * 0xFFFF))
level += 1;
return (level < ZSKIPLIST_MAXLEVEL) ? level : ZSKIPLIST_MAXLEVEL;
}`
这里的复杂度是O(N),但是应该是O(ZSKIPLIST_MAXLEVEL)。由于在算法中复杂度计算的时候会忽略常数,因此这个函数的复杂度其实是O(1),当然需要假设random的复杂度是O(1).
还有跳跃表的插入和删除在代码中都是O(Nlog(N)),而查找的复杂度是O(log(N))。但是跳跃表的插入和查找的复杂度其实是一样的,都是最好O(log(N)),最坏O(N)。这点维基也有写,希望您能改下。
当时看到这个地方的时候确实很让人迷惑。
The text was updated successfully, but these errors were encountered: