In
computer science
Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
(specifically
computational complexity theory
In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and explores the relationships between these classifications. A computational problem ...
), the worst-case complexity measures the
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 want ...
(e.g. running time,
memory
Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembe ...
) that an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
requires given an input of arbitrary size (commonly denoted as in
asymptotic notation). It gives an upper bound on the resources required by the algorithm.
In the case of running time, the worst-case
time complexity
In theoretical 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 ...
indicates the longest running time performed by an algorithm given ''any'' input of size , and thus guarantees that the algorithm will finish in the indicated period of time. The order of growth (e.g. linear,
logarithmic) of the worst-case complexity is commonly used to compare the
efficiency
Efficiency is the often measurable ability to avoid making mistakes or wasting materials, energy, efforts, money, and time while performing a task. In a more general sense, it is the ability to do things well, successfully, and without waste.
...
of two algorithms.
The worst-case complexity of an algorithm should be contrasted with its
average-case complexity, which is an average measure of the amount of resources the algorithm uses on a random input.
Definition
Given a
model of computation and an algorithm
that halts on each input
, the mapping
is called the time complexity of
if, for every
input string ,
halts after exactly
steps.
Since we usually are interested in the dependence of the
time complexity
In theoretical 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 ...
on different input lengths, abusing terminology, the time complexity is sometimes referred to the mapping
, defined by the maximal complexity
:
of inputs
with length or size
.
Similar definitions can be given for
space complexity, randomness complexity, etc.
Ways of speaking
Very frequently, the complexity
of an algorithm
is given in asymptotic
Big-O Notation, which gives its growth rate in the form
with a certain real valued comparison function
and the meaning:
* There exists a positive
real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
and a
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
such that
:
Quite frequently, the wording is:
* „Algorithm
has the worst-case complexity
.“
or even only:
* „Algorithm
has complexity
.“
Examples
Consider performing
insertion sort on
numbers on a
random-access machine
In computer science, random-access machine (RAM or RA-machine) is a model of computation that describes an abstract machine in the general class of register machines. The RA-machine is very similar to the counter machine but with the added capab ...
. The best-case for the algorithm is when the numbers are already sorted, which takes
steps to perform the task. However, the input in the worst-case for the algorithm is when the numbers are reverse sorted and it takes
steps to sort them; therefore the worst-case time-complexity of insertion sort is of
.
See also
*
Analysis of algorithms
References
*
Thomas H. Cormen,
Charles E. Leiserson,
Ronald L. Rivest, and
Clifford Stein.
Introduction to Algorithms
''Introduction to Algorithms'' is a book on computer programming by Thomas H. Cormen, Charles E. Leiserson, Ron Rivest, Ronald L. Rivest, and Clifford Stein. The book is described by its publisher as "the leading algorithms text in universities w ...
, Second Edition. MIT Press and McGraw-Hill, 2001. . Chapter 2.2: Analyzing algorithms, pp.25-27.
* Oded Goldreich. Computational Complexity: A Conceptual Perspective. Cambridge University Press, 2008. {{ISBN, 0-521-88473-X, p.32.
Analysis of algorithms