The Big O Notation for reading a textfile with 5-20 words
i have a algorithm that opens a textfile, reads between 5 and 20 words, store them into an array and closes the textfile again.
Has this algorithm开发者_运维百科 a Big O Natation (1) or (n)?
I'm going to go against common opinion here and say it's O(n)
where n
is the average word length. Clearly if the length of those 20 words doubles, so does the amount of work you need to do to read them.
If the maximum length of the words is also constant however, it will be O(1)
.
It's O(1) unless you tell us what n
is supposed to be.
If every single time the algorithm is run, it reads no more than 20 words, then it is O(1), as the time that it takes to run the algorithm does not get longer as the number of words in the text file increases.
O(1) it will always use a bounded number of operations.
精彩评论