How to calculate time and space complexity of algorithms [closed]
How to calculate space and time complexity of algorithms in java.
Is the total time for execution [ Using System.nanoTime() ] equals time complexity of any alogrithm or function ?
Example : Space and Time Complexity estimate of nth number in fibonacci series
Time complexity is a theoretical indication of scalability on an idealised machine. (its about the algorithim, not the machine)
System.nanoTime() will tell you how long something took on a specific machine, in a specific state for specific data inputs.
Time complexity is better for working out worst case values, measuring is more useful if you have a specific use case you want to consider.
Is the total time for execution [ Using System.nanoTime() ] equals time complexity of any alogrithm or function ?
No. When calculating the complexity order of a program, it is usually done in the Big-O notation. Here is everything you need to know about it. With examples.
Firstly you must define basic operation of the algorithm. Put a counter to calculate how many times basic operation works till your algorithm finihed working. Try to denote this counter as n. In fibonacci series, basic operation is addition(adding last two elements give you the next) To calculate nth number, n-1 addition must be done. So, complexity of fibonacci series is realized as O(n)
精彩评论