# How to Find Time Complexity of a Program in C

### Introduction:

Time Complexity analysis is an essential part of algorithm design and optimization. It is used to understand how runtime will increase proportionately to the input size. There are a lot of ways in C by which we can calculate the Time Complexity of a program. In this article, we will see some general techniques for finding the Time Complexity of a program in C.

## What is Time Complexity?

Time Complexity represents the time taken by a program or an algorithm when it is run as a function of the input. It denotes the speed at which the program's runtime changes as the input size changes. Time complexity is typically represented in Big O notation, which represents the upper bound of the runtime of the algorithm.

### How to measure the Time Complexity of a Program which is written in C Programming language?

To find the time complexity of a program in C, we need to follow the steps given below:

Step 1: Determine the Input Size of the Program

The input size of the program is the size of the input data that the program operates on. For example, if the program sorts an array of n integers, then the input size is n.

Step 2: Identify the Operations Performed in the Program

We need to identify the operations performed in the program and determine their execution time in terms of the input size. We can use the following table to determine the execution time of different operations:

OperationExecution Time
AssignmentConstant Time
ComparisonConstant Time
ArithmeticConstant Time
Function callDepends on the function being called
LoopDepends on the number of iterations
ConditionalsDepends on the number of branches
Array accessConstant Time
Pointer dereferenceConstant Time

Step 3: Analyze the Execution Time of the Program

We need to analyze the execution time of the program by determining the worst-case execution time of each operation in terms of the input size. We then sum up the execution time of all the operations to get the overall Time Complexity of the program.

There are various techniques for finding the time complexity of a program in C. Some of the commonly used techniques are as follows:

### Counting Operations:

One way to find the Time Complexity of a program is to Count the number of operations that are executed for a given input size. This technique is useful for simple algorithms that have a small number of operations. We can get the Time Complexity by getting the time to execute the single operation and multiplying it by the total number of operations.

Below is an example which will get the sum of the first n natural numbers in C.

C Code:

The number of operations in the for loop is n, and each operation takes constant time. That's why the Time Complexity of the above example is considered as O(n).

### Analyzing Loops:

Loops are a common source of complexity in algorithms. In a loop, the Time Complexity is dependent on the number of times the loop iterates, and, in each iteration, the complexity of the code which is inside the loop.

For example, consider the following C program that finds the maximum element in an array:

C Code:

The for loop iterates n-1 times, where n is the size of the array. The Time Complexity of the if statement inside the loop is constant. That's why the Time Complexity of the above example is considered as O(n).

### Recursion:

Recursion means a function calling itself to solve a bigger problem. When there is a bigger problem, and we need to solve it by dividing it into sub-problems, then we use Recursion. To analyze the Time Complexity of a Recursive algorithm, we need to consider the number of Recursive calls and the Time Complexity of the code inside the function.

For example, consider the following C program that calculates the factorial of a number using Recursion:

C Code:

The function is recursively called n-1 times so Time Complexity will be O(n).