冒泡/简单排序的运行时间

发布于 2024-12-13 01:52:34 字数 202 浏览 6 评论 0原文

在课堂上,简单排序的使用就像我们对 O(N) 运行时的第一个定义一样......

但是由于它每次运行时都会对数组进行一次更少的迭代,所以它不是更符合以下方式吗...

运行时气泡= sum(i = 0, n, (ni)) ?

而且,在渐近分析中,不仅是一个接一个地运行的最大进程才被计为 N 次迭代,为什么根据定义,这种排序不是 O(N) 呢?

In class, simple sort is used as like one of our first definitions of O(N) runtimes...

But since it goes through one less iteration of the array every time it runs, wouldn't it be something more along the lines of...

Runtime bubble= sum(i = 0, n, (n-i)) ?

And aren't only the biggest processes when run one after another counted in asymptotic analysis which would be the N iteration, why is by definition this sort not O(N)?

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

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

发布评论

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

评论(3

蒗幽 2024-12-20 01:52:34

1 + 2 + ... + N 的总和是 N*(N+1)/2 ...(高中数学)...并且接近(N^2)/2 随着 N 趋于无穷大。经典O(N^2)

The sum of 1 + 2 + ... + N is N*(N+1)/2 ... (high school maths) ... and that approaches (N^2)/2 as N goes to infinity. Classic O(N^2).

风流物 2024-12-20 01:52:34

我不确定你(或你的教授)从哪里得到冒泡排序是 O(n) 的概念。如果您的教授拥有一个有保证的 O(n) 排序算法,那么他们明智的做法是尝试并为其申请专利:-)

从本质上讲,冒泡排序是 O(n2 )。

这是因为它必须完整传递整个数据集,才能正确放置第一个元素。

然后第二遍 N - 1 元素以正确放置第二个。第三遍 N - 2 元素以正确放置第三个。

依此类推,实际上最终得到了接近 N * N / 2 次操作,删除了多余的 0.5 常量,复杂度为 O(n2 )。

I'm not sure where you (or your professor) got the notion that bubble sort is O(n). If your professor had a guaranteed O(n) sort algorithm, they'd be wise to try and patent it :-)

A bubble sort is, by it's very nature, O(n2).

That's because it has to make a full pass of the entire data set, to correctly place the first element.

Then a second pass of N - 1 elements to correctly place the second. And a third pass of N - 2 elements to correctly place the third.

And so on, effectively ending up with close to N * N / 2 operations which, removing the superfluous 0.5 constant, is O(n2).

菩提树下叶撕阳。 2024-12-20 01:52:34

冒泡排序的时间复杂度为O(n^2)。
考虑复杂度时,只考虑最大的表达式(而不考虑因子)

The time complexity of bubble sort is O(n^2).
When considering the complexity, only the largest expression is considered (but not the factor)

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