How to analyze time complexity: Count your steps (2024)

yourbasic.org

Time complexity esti­mates the time to run an algo­rithm.It's calcu­lated by counting elemen­tary opera­tions.

How to analyze time complexity: Count your steps (1)

  • Example (iterative algorithm)
  • Worst-case time complexity
  • Average-case time complexity
  • Quadratic time complexity

Example (iterative algorithm)

What’s the running time of the following algorithm?

// Compute the maximum element in the array a.Algorithm max(a):max ← a[0]for i = 1 to len(a)-1if a[i] > maxmax ← a[i]return max

The answer depends on factors such as input, programming language and runtime,coding skill, compiler, operating system, and hardware.

We often want to reason about execution time in a way that dependsonly on the algorithm and its input.This can be achieved by choosing an elementary operation,which the algorithm performs repeatedly, and definethe time complexity T(n) as the number of such operationsthe algorithm performs given an array of lengthn.

For the algorithm above we can choose the comparisona[i] > max as an elementary operation.

  1. This captures the running time of the algorithm well,since comparisons dominate all other operationsin this particular algorithm.
  2. Also, the time to perform a comparison is constant:it doesn’t depend on the size ofa.

The time complexity, measured in the number of comparisons,then becomes T(n)=n-1.

In general, an elementary operation must have two properties:

  1. There can’t be any other operations that are performed more frequentlyas the size of the input grows.
  2. The time to execute an elementary operation must be constant:it mustn’t increase as the size of the input grows.This is known as unitcost.

Worst-case time complexity

Consider this algorithm.

// Tell whether the array a contains x.Algorithm contains(a, x):for i = 0 to len(a)-1if x == a[i]return truereturn false

The comparison x == a[i] can be used as an elementary operation in this case.However, for this algorithm the number of comparisons depends not only on the number of elements, n,in the array but also on the value of x and the values ina:

  • If x isn’t found in a the algorithm makes n comparisons,
  • but if x equals a[0] there is only one comparison.

Because of this, we often choose to study worst-case time complexity:

  • Let T1(n), T2(n),… be the execution timesfor all possible inputs of sizen.
  • The worst-case time complexity W(n) is then defined asW(n)=max(T1(n), T2(n),…).

The worst-case time complexity for the contains algorithm thus becomesW(n)=n.

Worst-case time complexity gives an upper bound on time requirementsand is often easy to compute.The drawback is that it’s often overly pessimistic.

See Time complexity of array/list operationsfor a detailed look at the performance of basic array operations.

Average-case time complexity

Average-case time complexity is a less common measure:

  • Let T1(n), T2(n),… be the execution timesfor all possible inputs of sizen,
    and let P1(n), P2(n),… be the probabilitiesof these inputs.
  • The average-case time complexity is then defined asP1(n)T1(n) + P2(n)T2(n)+…

Average-case time is often harder to compute,and it also requires knowledge of how the input is distributed.

Quadratic time complexity

Finally, we’ll look at an algorithm with poor time complexity.

// Reverse the order of the elements in the array a.Algorithm reverse(a):for i = 1 to len(a)-1x ← a[i]for j = i downto 1a[j] ← a[j-1]a[0] ← x

We choose the assignment a[j] ← a[j-1] as elementary operation.Updating an element in an array is a constant-time operation,and the assignment dominates the cost of the algorithm.

The number of elementary operations is fully determined by the input sizen.In fact, the outer for loop is executed n-1 times.The time complexity therefore becomes

W(n)=1+2+…+(n-1)=n(n-1)/2=n2/2-n/2.

The quadratic term dominates for large n,and we therefore say that this algorithm has quadratic time complexity.This means that the algorithm scales poorly and can be used only for small input:to reverse the elements of an array with 10,000 elements,the algorithm will perform about 50,000,000 assignments.

In this case it’s easy to find an algorithm with linear time complexity.

Algorithm reverse(a):for i = 0 to n/2swap a[i] and a[n-i-1]

This is a huge improvement over the previous algorithm:an array with 10,000elements can now be reversedwith only 5,000swaps, i.e. 10,000assignments.That’s roughly a 5,000-fold speed improvement,and the improvement keeps growing as the the input gets larger.

It’s common to use Big O notationwhen talking about time complexity. We could then say thatthe time complexity of the first algorithm is Θ(n2),and that the improved algorithm has Θ(n) time complexity.

Further reading

Big O notation

Time complexity of array/list operations [Java, Python]

Time complexity of recursive functions [Master theorem]

Share this page:

How to analyze time complexity: Count your steps (2024)

FAQs

How to analyze time complexity: Count your steps? ›

The step count method is one of the methods to analyze the Time complexity of an algorithm. In this method, we count the number of times each instruction is executed. Based on that we will calculate the Time Complexity. The step Count method is also called as Frequency Count method.

What is the step count method for time complexity? ›

The step-count method is a measure to estimate the time complexity where the number of steps any program statement is to be assigned depends on the nature of that statement.

How do you analyze time complexity? ›

Time complexity is very useful measure in algorithm analysis. It is the time needed for the completion of an algorithm. To estimate the time complexity, we need to consider the cost of each fundamental instruction and the number of times the instruction is executed. Example 1: Addition of two scalar variables.

What is a step in complexity analysis? ›

Time Complexity

A "step" is an operation that takes constant time, such as a variable assignment, a comparison, an array access, an arithmetic function, and so on.

How to calculate step count? ›

Time spent doing activity x step conversion factor for activity = total step count for activity. So, if you did 20 minutes of yoga, your steps would be calculated in the following way: 20 minutes x 45 steps per minute = 900 steps.

What is a step count? ›

A pedometer, or step-counter, is a device, usually portable and electronic or electromechanical, that counts each step a person takes by detecting the motion of the person's hands or hips.

How to analyze worst case time complexity? ›

We define an algorithm's worst-case time complexity by using the Big-O notation, which determines the set of functions grows slower than or at the same rate as the expression. Furthermore, it explains the maximum amount of time an algorithm requires to consider all input values.

What is the best explanation of time complexity? ›

time complexity, a description of how much computer time is required to run an algorithm. In computer science, time complexity is one of two commonly discussed kinds of computational complexity, the other being space complexity (the amount of memory used to run an algorithm).

How do you deal with time complexity? ›

Let n be the main variable in the problem.
  1. If n ≤ 12, the time complexity can be O(n!).
  2. If n ≤ 25, the time complexity can be O(2n).
  3. If n ≤ 100, the time complexity can be O(n4).
  4. If n ≤ 500, the time complexity can be O(n3).
  5. If n ≤ 104, the time complexity can be O(n2).
  6. If n ≤ 106, the time complexity can be O(n log n).

What are the 3 levels of complexity? ›

Let's look at each of those in turn.
  • Structural complexity. This is the 'easiest' level of complexity and it involves the scale of the work on the project. ...
  • Emergent complexity. ...
  • Socio-political complexity.
Jan 8, 2014

What are the 4 categories of complexity? ›

According to project management experts Remington and Pollack, there are four types of complexity that determine the selection of projects. These include structural, technical, temporal, and directional complexity.

What is a realistic step count? ›

The average American walks 3,000 to 4,000 steps a day, or roughly 1.5 to 2 miles. It's a good idea to find out how many steps a day you walk now, as your own baseline. Then you can work up toward the goal of 10,000 steps by aiming to add 1,000 extra steps a day every two weeks.

Is 5000 steps a day sedentary? ›

The average U.S. adult takes 3,000 to 4,000 steps per day, which is the equivalent of about 1.5 to 2 miles. Walking less than 5,000 steps each day is considered sedentary.

Why do I count my steps? ›

Ever found yourself regularly counting the number of steps you take, counting and recounting the number of items in your grocery cart, or holding out for the clock to switch to a particular time to perform a certain task? These behaviors could all be a sign of Counting OCD, sometimes referred to as arithmomania.

What is the time complexity of binary search using step count method? ›

At step k , we need to search through an array of size at most n/(2^k) . And we need to find the smallest such k for which we have no subarray to search through. The time complexity of binary search is, therefore, O(logn). This is much more efficient than the linear time O(n), especially for large values of n.

What is the time complexity of bubble sort using step count? ›

Bubble Sort is not the most efficient sorting algorithm for large datasets. Its time complexity is O ( n 2 ) in the worst-case scenario, which means that it can be slow for large sets of data.

What is step count in the Fibonacci series? ›

Fibonacci n-Step Number
OEISsequence
11, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, ...
2A0000451, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
3A0000731, 1, 2, 4, 7, 13, 24, 44, 81, ...
4A0000781, 1, 2, 4, 8, 15, 29, 56, 108, ...
3 more rows

Top Articles
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 5517

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.