计算时间复杂度..需要帮助得出最终结果
为了明天的期中考试而学习,这些时间复杂性让我很挣扎。我将回顾书中的简单示例,对于此示例
交换排序,
void exchangesort (int n, keytype S[])
{
index i, j;
for(i=1; i<=n-1; i++)
for(j=i+1; j<=n; j++)
if(S[j] < S[i])
exchange S[i] and S[j];
}
对于此交换排序的“每种情况时间复杂性”,我理解我们几乎分析的部分j
for循环,因为它有基本操作(交换)。因此,如果您列出通过的总数,则为:
T(n) = (n-1) + (n-2) + (n-3) + ... + 1 = (n-1)n/2
现在我的问题是... 1 从哪里来?我以为是n-1 + n-2 +... + n
。
此外,我真正不明白的是如何得出 (n-1)n/2
。
这显然是我在期中必须想出的,通过观察,(n-1)n/2
并不直观......我明白如何想出T(n) = (n-1) + (n-2)
等等,我想......
有人可以用外行的术语向我解释一下,这样我就可以得出这样的答案明天期中考试?
Studying for a midterm tomorrow, and these time complexities are something I struggle with. I'm going over the simple examples in the book and for this example
Exchange Sort
void exchangesort (int n, keytype S[])
{
index i, j;
for(i=1; i<=n-1; i++)
for(j=i+1; j<=n; j++)
if(S[j] < S[i])
exchange S[i] and S[j];
}
For the "Every-Case Time Complexity" of this Exchange sort, I understand the part that we pretty much analyze the j
for-loop, because it has the basic operation (the exchange). And so if you list out the total number of passes, it's given by:
T(n) = (n-1) + (n-2) + (n-3) + ... + 1 = (n-1)n/2
Now my question is... where does the 1 come from? I thought it was n-1 + n-2 +... + n
.
Furthermore, what I really don't understand is how to come up with the (n-1)n/2
.
That's obviously what I have to come up with in the midterm, and by looking at that, (n-1)n/2
doesn't come intuitively... I understand how to come up with the T(n) = (n-1) + (n-2)
etc., I think...
Can someone explain this to me in laymen's terms so I can come up with an answer like this for my midterm tomorrow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在内循环中,
j
从i+1
运行到n
,即经过ni
值。总共有几个步骤,
(n-1) + (n-2) + ... + (n - (n-1)) = (n-1) + (n-2) + .. . 1.
.现在,对于前 k 个正整数的和,有一个封闭公式,就是
这里,k = n-1。
要计算前 k 个正整数之和的公式,有几种不错的方法,如 罗伯特·金的回答。据称,高斯在五岁的时候就用第一种方法算出来了,老师给学生们布置了计算1到100整数之和的任务,希望能有几分钟安静的时间。最好看看是否这样安排:
In the inner loop,
j
runs fromi+1
ton
, that is, throughn-i
values. So altogether, there aresteps,
(n-1) + (n-2) + ... + (n - (n-1)) = (n-1) + (n-2) + ... . 1
.Now, for the sum of the first
k
positive integers, there is a closed formula, it'sHere,
k = n-1
.To work out the formula for the sum of the first
k
positive integers, there are several nice ways, as mentioned in robert king's answer. Allegedly, Gauss worked it out in the first way when he was five years old and the teacher gave the pupils the task to calculate the sum of the integers from 1 to 100 in the hope of having a few quiet minutes. It's better to see if arranged thus:一种计算方法是:
sum=1+2+3+4+...+n
2*sum = 1+2+3+4+...+n + 1 + 2 + 3 + 4 +。 ..+n = 1+(n) + 2+(n-1) + 3+(n-2)..+ (n)+1
2*总和=1+n + 1+n + 1+n。 ..
2*总和 = n(1+n)
sum=n*(n+1)/2
但更简单的理解方法是想象一个正方形或网格矩阵。
当 i 向下移动到每个新行时,j 会穿过矩阵对角线的额外列(因为 i<=j .. 我们知道 j 无法超过对角线)。
这意味着所有操作都是矩阵对角线一侧 (i,j) 的组合。
因此,运算次数是整个矩阵面积的一半。如果矩阵是一个 *n 矩阵,那么我们大约有 n*n/2 面积(但由于我们不能将对角线数两倍,所以实际上是 n*(n-1)/2
one way of working it out is:
sum=1+2+3+4+...+n
2*sum = 1+2+3+4+...+n + 1 + 2 + 3 + 4 +...+n = 1+(n) + 2+(n-1) + 3+(n-2)..+ (n)+1
2*sum=1+n + 1+n + 1+n...
2*sum = n(1+n)
sum=n*(n+1)/2
but an easier way to see this is to imagine a square or a grid matrix.
as i goes down to each new row, j goes across and extra column to the diagonal of the matrix (as i<=j .. we know j can't get past the diagonal).
This means all the operations are a combination of (i,j) on one side of the matrix's diagonal.
The number of operations is therefore half the area of the entire matrix. if the matrix is a n*n matrix, then we have about n*n/2 area (but since we can't count the diagonal twice its actually n*(n-1)/2
好的,这样想:
现在总结一下:
在最坏的情况下,
交换
将进行n(n - 1) / 2
次。OK, think like this:
Now sum them up:
In the worst case, the
exchange
will be donen(n - 1) / 2
times.该级数从 (n-1) 到 (1) [(n-1), (n-2) ... (n-(n-1))]
请此处查看该系列的总和,了解它是如何得出的。它很容易理解。
The series goes from (n-1) to (1) [(n-1), (n-2) ... (n-(n-1))]
check the sum of the series here to know how it was derived. Its pretty easy to understand.