对 Big-O 表示法感到困惑

发布于 2025-01-04 09:37:31 字数 1434 浏览 4 评论 0原文

好吧,我有一个简单的问题要问所有正在准备回答一个简单问题的程序员。

在我的计算机科学 II 课上,我们将学习 Big-Oh 表示法,我已经记下了大部分内容,但我仍然对示例的一些语义感到困惑。

这里的一切都是用 Java 编写的。

我的教授在课堂上给了我们这些例子,但幸运的是,我没有写下答案。

a)

int count = 0; 
    for (int i = 1; i <= n; i++) 
        for (int j = n; j > 1; j-­--­-) 
            for (int k = 1; k < n; k = k + 2) 
                count++;

b)

int count = 0; 
for (int i = 1; i <= n; i++) 
    for (int j = n; j > 1; j-­--­-) 
        for (int k = 1; k < 1000; k = k + 2) 
            count++;

c)

int count = 0; 
    for (int i = 1; i <= 1000000; i++) 
        for (int j = i; j > 500; j-­--­-) 
            for (int k = 1; k < 10500; k = k + 2) 
                count++;

d)

int count = 0; 
int j = 1; 
for (int i = 1; i < n; i++) { 
        while (j < n) { 
            j++;
            count++; 
        } 
        j = 1; 
    }

e)

int count = 0; 
int i = n; 
while (i > 1) 
{ 
    count++; i = i / 2; 
}

好吧,这是我的答案/思考过程:

a) N * N * (N / 2) = N^3/2,全部简化为 O(N^3) 表示法

b ) N * N * 500,全部简化为 O(N^2)

c) 这是我最困惑的一个,因为你有三个 for 循环,但所有循环都迭代了确切的次数。我的猜测是 O(1),但我不知道...

d) N * N = N^2,所以 O(N^2)

e) 每次除以一半,所以 log(n) = O( log(n)) [均为基数 2]

那么任何人都可以检查我的思维过程并看看我是否遗漏了什么?提前非常感谢!

Alright I have a quick question for all of the programmers that are preusing for an easy question.

For my Computer Science II class we are going over Big-Oh notation and I've got most of it down, but I am still confused on some of the semantics of the examples.

Everything here is written in Java.

My professor gave us these examples in class, but my luck, I didn't write down the answers.

a)

int count = 0; 
    for (int i = 1; i <= n; i++) 
        for (int j = n; j > 1; j-­--­-) 
            for (int k = 1; k < n; k = k + 2) 
                count++;

b)

int count = 0; 
for (int i = 1; i <= n; i++) 
    for (int j = n; j > 1; j-­--­-) 
        for (int k = 1; k < 1000; k = k + 2) 
            count++;

c)

int count = 0; 
    for (int i = 1; i <= 1000000; i++) 
        for (int j = i; j > 500; j-­--­-) 
            for (int k = 1; k < 10500; k = k + 2) 
                count++;

d)

int count = 0; 
int j = 1; 
for (int i = 1; i < n; i++) { 
        while (j < n) { 
            j++;
            count++; 
        } 
        j = 1; 
    }

e)

int count = 0; 
int i = n; 
while (i > 1) 
{ 
    count++; i = i / 2; 
}

Alright so here are my answers/thought processes:

a) N * N * (N / 2) = N^3/2, All simplifies to a O(N^3) notation

b) N * N * 500, All simplifies to O(N^2)

c) This is the one that I am majorly confused on you have three for loops, but all iterate the exact number of times. My guess here is O(1), but I have no idea...

d) N * N = N^2, So O(N^2)

e) Divides by half each time, so log(n) = O(log(n)) [both base 2]

So can anyone check through my thought process and see if I am missing anything? Thank you so much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2025-01-11 09:37:31

是的,(C) 是 O(1),因为它都是常数。

Yeah (C) is O(1) because it's all constants.

我恋#小黄人 2025-01-11 09:37:31

在所有这些示例中,count 对于每个工作单元都会递增一次。如果你能弄清楚countn之间的关系,那么你就知道了程序的渐近顺序。

很高兴你把事情解决了。下一步是根据实际情况检查您的计算。针对不同的 n 值运行代码,打印出 count,然后根据实际数据检查您的工作。

In all these examples, count is incremented once for each work unit. If you can figure out the relationship between count and n, then you know the asymptotic order of the program.

It's good you worked things out. The next step is to check your calculations against reality. Run the code for different values of n, print out count, and check your work against real data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文