What is Big O of a loop?

前端 未结 6 831
离开以前
离开以前 2020-12-03 15:02

I was reading about Big O notation. It stated,

The big O of a loop is the number of iterations of the loop into number of statement

6条回答
  •  青春惊慌失措
    2020-12-03 15:22

    Usually big O notation expresses the number of principal operations in a function.

    In this tou're overating over n elements. So complexity is O(n).

    Surely is not O(n^2), since quadratic is the complexity of those algorithms, like bubble sort which compare every element in the input with all other elements.

    As you remember, bubble sort, in order to determine the right position in which to insert an element, compare every element with the others n in a list (bubbling behaviour).

    At most, you can claim that you're algorithm has complexity O(2n),since it prints 2 phrases for every element in the input, but in big O notation O(n) is quiv to O(2n).

提交回复
热议问题