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

2020-02-16 17:32发布

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(n):

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

O(n^2):
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // do stuff
            a[i][j] = INT;
        }
    }
  • What code can O(log(n)) describe?

Another question:

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

7条回答
Fickle 薄情
2楼-- · 2020-02-16 18:06

For O(logn), please have a look at any code that involves divide and conquer strategy Example: Merge sort & quick sort(expected running time is O(nlogn) in these cases)

查看更多
登录 后发表回答