An algorithm is said to be constant time (also written as O(1) time) if the value of T(n) is bounded by a value that does not depend on the size of the input. For example, accessing any single element in an array takes constant time as only one operation has to be performed to locate it. However, finding the minimal value in an unordered array is not a constant time operation as a scan over each element in the array is needed in order to determine the minimal value. Hence it is a linear time operation, taking O(n) time. If the number of elements is known in advance and does not change, however, such an algorithm can still be said to run in constant time.
Despite the name "constant time", the running time does not have to be independent of the problem size, but an upper bound for the running time has to be bounded independently of the problem size. For example, the task "exchange the values of a and b if necessary so that a≤b" is called constant time even though the time may depend on whether or not it is already true that a ≤ b. However, there is some constant t such that the time required is always at most t.
Here are some examples of code fragments that run in constant time:
int index = 5; int item = list; if (condition true) then perform some operation that runs in constant time else perform some other operation that runs in constant time for i = 1 to 100 for j = 1 to 200 perform some operation that runs in constant timeIf T(n) is O(any constant value), this is equivalent to and stated in standard notation as T(n) being O(1).
Read more about this topic: Time Complexity
Famous quotes containing the words constant and/or time:
“Love not me for comely grace,
For my pleasing eye or face,
Nor for any outward part:
No, nor for a constant heart!”
—Unknown. Love Not Me for Comely Grace (l. 14)
“I cannot exaggerate the waste of the Presidents time and the consumption of his nervous vitality involved in listening to congressmens intercession as to local appointments. Why should the President have to have his time taken up in a discussion over ... who shall be the postmistress of Devils Lake, in North Dakota? How should he be able to know ... who is best fitted to fill such a place?”
—William Howard Taft (18571930)