Big O - O(log(n)) code example

后端 未结 7 1257
予麋鹿
予麋鹿 2020-12-23 12:37

Like the Big O notation \"O(1)\" can describe following code:

O(1):

    for (int i = 0; i < 10; i++) {
        // do stuff 
        a[i] = INT;
    }

O         


        
7条回答
  •  我在风中等你
    2020-12-23 12:51

    It might be worth emphasizing that the lower complexity algorithms you described are subsets of the the higher complexity ones. In other words,

    for (int i = 0; i < 10; i++) {
        // do stuff 
        a[i] = INT;
    }
    

    is in O(1), but also in O(n), O(n²), and, if you wanted to be clever, O(log(n)).Why? Because all constant time algorithms are bounded by some linear, quadratic, etc. functions.

    What solutions are there for "Big O problems" (what to do, when getting a lot of data as an input)?

    This question doesn't make a lot of sense to me. "A lot of data" is a quite arbitrary. Still, keep in mind that Big O isn't the only measure of time complexity; apart from measuring worst case time complexity, we can also examine best-case and average case, though these can be a bit trickier to calculate.

提交回复
热议问题