In

_{0} and a constant , the run-time of that algorithm will never be larger than . This concept is frequently expressed using Big O notation. For example, since the run-time of

_{1}, step 2 uses time ''T''_{2}, and so forth.
In the algorithm above, steps 1, 2 and 7 will only be run once. For a worst-case evaluation, it should be assumed that step 3 will be run as well. Thus the total amount of time to run steps 1-3 and step 7 is:
:$T\_1\; +\; T\_2\; +\; T\_3\; +\; T\_7.\; \backslash ,$
The loops in steps 4, 5 and 6 are trickier to evaluate. The outer loop test in step 4 will execute ( ''n'' + 1 )
times (note that an extra step is required to terminate the for loop, hence n + 1 and not n executions), which will consume ''T''_{4}( ''n'' + 1 ) time. The inner loop, on the other hand, is governed by the value of j, which iterates from 1 to ''i''. On the first pass through the outer loop, j iterates from 1 to 1: The inner loop makes one pass, so running the inner loop body (step 6) consumes ''T''_{6} time, and the inner loop test (step 5) consumes 2''T''_{5} time. During the next pass through the outer loop, j iterates from 1 to 2: the inner loop makes two passes, so running the inner loop body (step 6) consumes 2''T''_{6} time, and the inner loop test (step 5) consumes 3''T''_{5} time.
Altogether, the total time required to run the inner loop body can be expressed as an ^{2} is the highest-order term, so one can conclude that . Formally this can be proven as follows:
A more elegant approach to analyzing this algorithm would be to declare that 1..''T''7">'T''_{1}..''T''_{7}are all equal to one unit of time, in a system of units chosen so that one unit is greater than or equal to the actual times for these steps. This would mean that the algorithm's run-time breaks down as follows:This approach, unlike the above approach, neglects the constant time consumed by the loop tests which terminate their respective loops, but it is trivial to prove that such omission does not affect the final result

^{32} = 4 GiB (greater if segmented memory is used) and on 64-bit machines 2^{64} = 16 EiB. Thus given a limited size, an order of growth (time or space) can be replaced by a constant factor, and in this sense all practical algorithms are for a large enough constant, or for small enough data.
This interpretation is primarily useful for functions that grow extremely slowly: (binary) iterated logarithm (log^{*}) is less than 5 for all practical data (2^{65536} bits); (binary) log-log (log log ''n'') is less than 6 for virtually all practical data (2^{64} bits); and binary log (log ''n'') is less than 64 for virtually all practical data (2^{64} bits). An algorithm with non-constant complexity may nonetheless be more efficient than an algorithm with constant complexity on practical data if the overhead of the constant time algorithm results in a larger constant factor, e.g., one may have $K\; >\; k\; \backslash log\; \backslash log\; n$ so long as $K/k\; >\; 6$ and $n\; <\; 2^\; =\; 2^$.
For large data linear or quadratic factors cannot be ignored, but for small data an asymptotically inefficient algorithm may be more efficient. This is particularly used in hybrid algorithms, like Timsort, which use an asymptotically efficient algorithm (here merge sort, with time complexity $n\; \backslash log\; n$), but switch to an asymptotically inefficient algorithm (here

computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...

, the analysis of algorithms is the process of finding the computational complexity
In computer science, the computational complexity or simply complexity of an algorithm is the amount of resources required to run it. Particular focus is given to time complexity, computation time (generally measured by the number of needed elemen ...

of algorithms—the amount of time, storage, or other resources needed to execute them. Usually, this involves determining a function that relates the size of an algorithm's input to the number of steps it takes (its time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by t ...

) or the number of storage locations it uses (its space complexity). An algorithm is said to be efficient when this function's values are small, or grow slowly compared to a growth in the size of the input. Different inputs of the same size may cause the algorithm to have different behavior, so best, worst and average case descriptions might all be of practical interest. When not otherwise specified, the function describing the performance of an algorithm is usually an upper bound
In mathematics, particularly in order theory, an upper bound or majorant of a subset of some Preorder, preordered set is an element of that is greater than or equal to every element of .
Duality (order theory), Dually, a lower bound or minora ...

, determined from the worst case inputs to the algorithm.
The term "analysis of algorithms" was coined by Donald Knuth
Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the Acm Turing award, ACM Turing Award, informally considered the Nobel Pri ...

. Algorithm analysis is an important part of a broader computational complexity theory
In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and relating these classes to each other. A computational problem is a task solved by ...

, which provides theoretical estimates for the resources needed by any algorithm which solves a given computational problem
In theoretical computer science, a computational problem is a problem that may be solved by an algorithm. For example, the problem of factoring
:"Given a positive integer ''n'', find a nontrivial prime factor of ''n''."
is a computational probl ...

. These estimates provide an insight into reasonable directions of search for efficient algorithms.
In theoretical analysis of algorithms it is common to estimate their complexity in the asymptotic sense, i.e., to estimate the complexity function for arbitrarily large input. Big O notation
Big ''O'' notation is a mathematical notation that describes the asymptotic analysis, limiting behavior of a function (mathematics), function when the Argument of a function, argument tends towards a particular value or infinity. Big O is a memb ...

, Big-omega notation and Big-theta notation are used to this end. For instance, binary search
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...

is said to run in a number of steps proportional to the logarithm of the size of the sorted list being searched, or in , colloquially "in logarithmic time". Usually asymptotic
In analytic geometry, an asymptote () of a curve is a line such that the distance between the curve and the line approaches zero as one or both of the ''x'' or ''y'' coordinates Limit of a function#Limits at infinity, tends to infinity. In pro ...

estimates are used because different implementation
Implementation is the realization of an application, or execution of a plan, idea, scientific modelling, model, design, specification, Standardization, standard, algorithm, or policy.
Industry-specific definitions
Computer science
In computer ...

s of the same algorithm may differ in efficiency. However the efficiencies of any two "reasonable" implementations of a given algorithm are related by a constant multiplicative factor called a ''hidden constant''.
Exact (not asymptotic) measures of efficiency can sometimes be computed but they usually require certain assumptions concerning the particular implementation of the algorithm, called model of computation
In computer science, and more specifically in computability theory (computer science), computability theory and computational complexity theory, a model of computation is a model which describes how an output of a function (mathematics), mathema ...

. A model of computation may be defined in terms of an abstract computer, e.g. Turing machine
A Turing machine is a mathematical model of computation describing an abstract machine that manipulates symbols on a strip of tape according to a table of rules. Despite the model's simplicity, it is capable of implementing any computer algori ...

, and/or by postulating that certain operations are executed in unit time.
For example, if the sorted list to which we apply binary search has elements, and we can guarantee that each lookup of an element in the list can be done in unit time, then at most time units are needed to return an answer.
Cost models

Time efficiency estimates depend on what we define to be a step. For the analysis to correspond usefully to the actual run-time, the time required to perform a step must be guaranteed to be bounded above by a constant. One must be careful here; for instance, some analyses count an addition of two numbers as one step. This assumption may not be warranted in certain contexts. For example, if the numbers involved in a computation may be arbitrarily large, the time required by a single addition can no longer be assumed to be constant. Two cost models are generally used:, section 1.3 * the uniform cost model, also called uniform-cost measurement (and similar variations), assigns a constant cost to every machine operation, regardless of the size of the numbers involved * the logarithmic cost model, also called logarithmic-cost measurement (and similar variations), assigns a cost to every machine operation proportional to the number of bits involved The latter is more cumbersome to use, so it's only employed when necessary, for example in the analysis of arbitrary-precision arithmetic algorithms, like those used incryptography
Cryptography, or cryptology (from grc, , translit=kryptós "hidden, secret"; and ''graphein'', "to write", or ''-logy, -logia'', "study", respectively), is the practice and study of techniques for secure communication in the presence of ...

.
A key point which is often overlooked is that published lower bounds for problems are often given for a model of computation that is more restricted than the set of operations that you could use in practice and therefore there are algorithms that are faster than what would naively be thought possible.
Run-time analysis

Run-time analysis is a theoretical classification that estimates and anticipates the increase in '' running time'' (or run-time or execution time) of analgorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...

as its '' input size'' (usually denoted as ) increases. Run-time efficiency is a topic of great interest in computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...

: A program can take seconds, hours, or even years to finish executing, depending on which algorithm it implements. While software profiling techniques can be used to measure an algorithm's run-time in practice, they cannot provide timing data for all infinitely many possible inputs; the latter can only be achieved by the theoretical methods of run-time analysis.
Shortcomings of empirical metrics

Since algorithms are platform-independent (i.e. a given algorithm can be implemented in an arbitraryprogramming language
A programming language is a system of notation for writing computer program, computer programs. Most programming languages are text-based formal languages, but they may also be visual programming language, graphical. They are a kind of computer ...

on an arbitrary computer
A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations (computation) automatically. Modern digital electronic computers can perform generic sets of operations known as Computer program, pr ...

running an arbitrary operating system
An operating system (OS) is system software that manages computer hardware, software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ef ...

), there are additional significant drawbacks to using an empirical
Empirical evidence for a proposition is evidence, i.e. what supports or counters this proposition, that is constituted by or accessible to sense experience or experimental procedure. Empirical evidence is of central importance to the sciences and ...

approach to gauge the comparative performance of a given set of algorithms.
Take as an example a program that looks up a specific entry in a sorted list
A ''list'' is any set of items in a row. List or lists may also refer to:
People
* List (surname)
Organizations
* List College, an undergraduate division of the Jewish Theological Seminary of America
* SC Germania List, German rugby uni ...

of size ''n''. Suppose this program were implemented on Computer A, a state-of-the-art machine, using a linear search algorithm, and on Computer B, a much slower machine, using a binary search algorithm
In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...

. Benchmark testing on the two computers running their respective programs might look something like the following:
Based on these metrics, it would be easy to jump to the conclusion that ''Computer A'' is running an algorithm that is far superior in efficiency to that of ''Computer B''. However, if the size of the input-list is increased to a sufficient number, that conclusion is dramatically demonstrated to be in error:
Computer A, running the linear search program, exhibits a linear
Linearity is the property of a mathematical relationship ('' function'') that can be graphically represented as a straight line
Line most often refers to:
* Line (geometry)
In geometry, a line is an infinitely long object with no width, ...

growth rate. The program's run-time is directly proportional to its input size. Doubling the input size doubles the run-time, quadrupling the input size quadruples the run-time, and so forth. On the other hand, Computer B, running the binary search program, exhibits a logarithm
In mathematics, the logarithm is the inverse function to exponentiation. That means the logarithm of a number to the base is the exponent to which must be raised, to produce . For example, since , the ''logarithm base'' 10 of ...

ic growth rate. Quadrupling the input size only increases the run-time by a constant amount (in this example, 50,000 ns). Even though Computer A is ostensibly a faster machine, Computer B will inevitably surpass Computer A in run-time because it's running an algorithm with a much slower growth rate.
Orders of growth

Informally, an algorithm can be said to exhibit a growth rate on the order of amathematical function
In mathematics, a function from a set (mathematics), set to a set assigns to each element of exactly one element of .; the words map, mapping, transformation, correspondence, and operator are often used synonymously. The set is called the D ...

if beyond a certain input size , the function times a positive constant provides an upper bound or limit for the run-time of that algorithm. In other words, for a given input size greater than some insertion sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time Comparison sort, by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or m ...

grows quadratically as its input size increases, insertion sort can be said to be of order .
Big O notation is a convenient way to express the worst-case scenario
A worst-case scenario is a concept in risk management wherein the planner, in planning for potential disasters, considers the most severe possible outcome that can reasonably be projected to occur in a given situation. Conceiving of worst-case sc ...

for a given algorithm, although it can also be used to express the average-case — for example, the worst-case scenario for quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...

is , but the average-case run-time is .
Empirical orders of growth

Assuming the run-time follows power rule, , the coefficient can be found by taking empirical measurements of run-time at some problem-size points , and calculating so that . In other words, this measures the slope of the empirical line on the log–log plot of run-time vs. input size, at some size point. If the order of growth indeed follows the power rule (and so the line on the log–log plot is indeed a straight line), the empirical value of will stay constant at different ranges, and if not, it will change (and the line is a curved line)—but still could serve for comparison of any two given algorithms as to their ''empirical local orders of growth'' behaviour. Applied to the above table: It is clearly seen that the first algorithm exhibits a linear order of growth indeed following the power rule. The empirical values for the second one are diminishing rapidly, suggesting it follows another rule of growth and in any case has much lower local orders of growth (and improving further still), empirically, than the first one.Evaluating run-time complexity

The run-time complexity for the worst-case scenario of a given algorithm can sometimes be evaluated by examining the structure of the algorithm and making some simplifying assumptions. Consider the followingpseudocode
In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machi ...

:
1 ''get a positive integer n from input''
2 if n > 10
3 print "This might take a while..."
4 for i = 1 to n
5 for j = 1 to i
6 print i * j
7 print "Done!"
A given computer will take a discrete amount of time to execute each of the instructions involved with carrying out this algorithm. The specific amount of time to carry out a given instruction will vary depending on which instruction is being executed and which computer is executing it, but on a conventional computer, this amount will be deterministic
Determinism is a Philosophy, philosophical view, where all events are determined completely by previously existing causes. Deterministic theories throughout the history of philosophy have developed from diverse and sometimes overlapping motive ...

. Say that the actions carried out in step 1 are considered to consume time ''T''arithmetic progression
An arithmetic progression or arithmetic sequence () is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with a common differ ...

:
:$T\_6\; +\; 2T\_6\; +\; 3T\_6\; +\; \backslash cdots\; +\; (n-1)\; T\_6\; +\; n\; T\_6$
which can be factored as
:$\backslash left;\; href="/html/ALL/s/1\_+\_2\_+\_3\_+\_\backslash cdots\_+\_(n-1)\_+\_n\_\backslash right.html"\; ;"title="1\; +\; 2\; +\; 3\; +\; \backslash cdots\; +\; (n-1)\; +\; n\; \backslash right">1\; +\; 2\; +\; 3\; +\; \backslash cdots\; +\; (n-1)\; +\; n\; \backslash right$
The total time required to run the outer loop test can be evaluated similarly:
:$\backslash begin\; \&\; 2T\_5\; +\; 3T\_5\; +\; 4T\_5\; +\; \backslash cdots\; +\; (n-1)\; T\_5\; +\; n\; T\_5\; +\; (n\; +\; 1)\; T\_5\backslash \backslash \; =\backslash \; \&T\_5\; +\; 2T\_5\; +\; 3T\_5\; +\; 4T\_5\; +\; \backslash cdots\; +\; (n-1)T\_5\; +\; nT\_5\; +\; (n+1)T\_5\; -\; T\_5\; \backslash end$
which can be factored as
:$\backslash begin\; \&\; T\_5\; \backslash left;\; href="/html/ALL/s/1+2+3+\backslash cdots\_+\_(n-1)\_+\_n\_+\_(n\_+\_1)\_\backslash right.html"\; ;"title="1+2+3+\backslash cdots\; +\; (n-1)\; +\; n\; +\; (n\; +\; 1)\; \backslash right">1+2+3+\backslash cdots\; +\; (n-1)\; +\; n\; +\; (n\; +\; 1)\; \backslash right$
Therefore, the total run-time for this algorithm is:
:$f(n)\; =\; T\_1\; +\; T\_2\; +\; T\_3\; +\; T\_7\; +\; (n\; +\; 1)T\_4\; +\; \backslash left;\; href="/html/ALL/s/\backslash frac\_(n^2\_+\_n)\_\backslash right.html"\; ;"title="\backslash frac\; (n^2\; +\; n)\; \backslash right">\backslash frac\; (n^2\; +\; n)\; \backslash right$
which reduces to
:$f(n)\; =\; \backslash left;\; href="/html/ALL/s/\backslash frac\_(n^2\_+\_n)\_\backslash right.html"\; ;"title="\backslash frac\; (n^2\; +\; n)\; \backslash right">\backslash frac\; (n^2\; +\; n)\; \backslash right$
As a rule-of-thumb, one can assume that the highest-order term in any given function dominates its rate of growth and thus defines its run-time order. In this example, nGrowth rate analysis of other resources

The methodology of run-time analysis can also be utilized for predicting other growth rates, such as consumption of memory space. As an example, consider the following pseudocode which manages and reallocates memory usage by a program based on the size of afile
File or filing may refer to:
Mechanical tools and processes
* File (tool), a tool used to ''remove'' fine amounts of material from a workpiece
**Filing (metalworking), a material removal process in manufacturing
** Nail file, a tool used to gent ...

which that program manages:
while ''file is still open:''
let n = ''size of file''
for ''every 100,000 kilobyte
The kilobyte is a multiple of the unit byte for Computer data storage, digital information.
The International System of Units (SI) defines the prefix ''kilo-, kilo'' as 1000 (103); per this definition, one kilobyte is 1000 bytes.International S ...

s of increase in file size''
''double the amount of memory reserved''
In this instance, as the file size n increases, memory will be consumed at an exponential growth
Exponential growth is a process that increases quantity over time. It occurs when the instantaneous Rate (mathematics)#Of change, rate of change (that is, the derivative) of a quantity with respect to time is proportionality (mathematics), propor ...

rate, which is order . This is an extremely rapid and most likely unmanageable growth rate for consumption of memory resources
Resource refers to all the materials available in our environment which are Technology, technologically accessible, Economics, economically feasible and Culture, culturally Sustainability, sustainable and help us to satisfy our needs and wants. R ...

.
Relevance

Algorithm analysis is important in practice because the accidental or unintentional use of an inefficient algorithm can significantly impact system performance. In time-sensitive applications, an algorithm taking too long to run can render its results outdated or useless. An inefficient algorithm can also end up requiring an uneconomical amount of computing power or storage in order to run, again rendering it practically useless.Constant factors

Analysis of algorithms typically focuses on the asymptotic performance, particularly at the elementary level, but in practical applications constant factors are important, and real-world data is in practice always limited in size. The limit is typically the size of addressable memory, so on 32-bit machines 2insertion sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time Comparison sort, by comparisons. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or m ...

, with time complexity $n^2$) for small data, as the simpler algorithm is faster on small data.
See also

*Amortized analysis
In computer science, amortized analysis is a method for Analysis of algorithms, analyzing a given algorithm's Computational complexity, complexity, or how much of a resource, especially time or memory, it takes to Execution (computing), execute. ...

* Analysis of parallel algorithms
* Asymptotic computational complexity
* Best, worst and average case
* Big O notation
Big ''O'' notation is a mathematical notation that describes the asymptotic analysis, limiting behavior of a function (mathematics), function when the Argument of a function, argument tends towards a particular value or infinity. Big O is a memb ...

* Computational complexity theory
In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and relating these classes to each other. A computational problem is a task solved by ...

* Master theorem (analysis of algorithms)
In the analysis of algorithms, the master theorem for divide-and-conquer recurrences provides an asymptotic analysis (using Big O notation) for recurrence relations of types that occur in the Analysis of algorithms, analysis of many divide and con ...

* NP-Complete
In computational complexity theory, a problem is NP-complete when:
# it is a problem for which the correctness of each solution can be verified quickly (namely, in polynomial time) and a brute-force search algorithm can find a solution by trying ...

* Numerical analysis
Numerical analysis is the study of algorithms that use numerical approximation (as opposed to symbolic computation, symbolic manipulations) for the problems of mathematical analysis (as distinguished from discrete mathematics). It is the study of ...

* Polynomial time
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by t ...

* Program optimization
* Profiling (computer programming)
In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time Computational complexity theory, complexity of a program, the instruction ...

* Scalability
Scalability is the property of a system to handle a growing amount of work by adding resources to the system.
In an economics, economic context, a scalable business model implies that a company can increase sales given increased resources. For ...

* Smoothed analysis
* Termination analysis — the subproblem of checking whether a program will terminate at all
* Time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by t ...

— includes table of orders of growth for common algorithms
* Information-based complexity
Notes

References

* * * * * *External links

* {{DEFAULTSORT:Analysis Of Algorithms Computational complexity theory