Home  >  Q&A  >  body text

c++ - 算法时间复杂度计算

请问有人知道下面算法的时间复杂度是多少吗?,如果知道,希望您能及时的帮一下忙。如果没有if 语句,单纯的两个嵌套循环,可以知道时间 复杂度为O(n^2),
for (i = 0; i < num; i++)
{
for (int j = 0; j < num; j++)
if (dataItem[i] == dataItem[j])
{
cou++;
if (cou > num / 2)
break;
}
if (cou > num / 2)
break;
}

PHP中文网PHP中文网2764 days ago526

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 15:04:41

    O(n^2)
    You can think of Big O Notation as representing the worst-case time complexity.
    In the worst case, all dataItems are not equal, and the loop will be executed approximately n^2/2 + n/2 times.

    reply
    0
  • Cancelreply