int currentMinIndex = 0;
for (int front = 0; front < intArray.length; front++)
{
currentMinIndex = front;
for (int i = front; i < intArray.length; i++)
{
if (intArray[i] < intArray[currentMinIndex])
{
currentMinIndex = i;
}
}
int tmp = intArray[front];
intArray[front] = intArray[currentMinIndex];
intArray[currentMinIndex] = tmp;
}
内循环迭代:n +(n-1)+(n-2)+(n-3)+ ... + 1次。为什么冒泡排序O(n^2)?
外循环迭代:n次。
所以,你得到N *
是不是N *(N *(N + 1)/ 2)(数字1到n的总和)= N *((N^2)+ n/2)
这将是(n^3)+(n^2)/ 2 = O(n^3)?
我很积极我做错了。为什么不是O(n^3)?
你正在计算外'n'两次。你的内部循环本身是O(n)。 – 2012-07-13 20:20:40
不要挑剔,但你显示的算法是[选择排序](http://en.wikipedia.org/wiki/Selection_sort)不是[泡泡排序](http://en.wikipedia.org/wiki/Bubble_sort ) – 2012-07-13 20:33:16
上周,我写了一篇关于渐近复杂性的文章,巧合之处在于,我以泡泡排序为例。给它一个镜头:-)(http://en.algoritmy.net/article/44682/Asymptotic-complexity)。正如亨克所说的那样,你的错误是内层循环是O(n)。 O(n^2) - 算术顺序的总和是两个循环的复杂度。 – malejpavouk 2012-07-13 20:39:14