# Time and Space Analysis of Algorithm

Learn: In this article we are going to study about the time and space analysis of any algorithm. What are time and space complexity of an algorithm? What are various ways to analyze program? Prior analysis and posteriori testing of an algorithm? Why do we analyze algorithm?
Submitted by Amit Shukla, on September 30, 2017

## Algorithm complexity

There are basically two aspects of computer programming. One is the data organization i.e. the data and structure to represent the data of the problem in hand, and is the subject of present text. The other one involves choosing the appropriate algorithm to solve the problem in hand. Data structure and algorithm designing, both involved with each other.

As an algorithm is a sequence of steps to solve a problem, there may be more than one algorithm to solve a problem.

The choice of particular algorithm depends upon the following considerations:

1. Performance required i.e., time complexity.
2. Memory requirement i.e., space complexity.
3. Programming requirements.

We can directly consider only time complexity and space complexity directly and programming requirements differ from language to language.

### 1. Space Complexity

The complexity of an algorithm, i.e., a program is the amount of memory; it needs to run to completion. Some of the reasons for studying space complexities are:

• If the program is to run on multi user system, it may be required to specify amount of memory to be allocated to the program.
• We may be interested to know in advance that weather sufficient memory available to run program.
• There may be several possible solutions with different space requirements.

The space needed by a program consist of the following components:

• Instruction space: Space needed to store the executable version of the program and it is fixed.
• Data space: Space needed to store all constants; variable values and has further following components:
• Space needed by constants and simple variables. This space is fixed.
• Space needed by fixed size structured variable, such as array and structure.
• Dynamically allocated space. This space usually varies.

The amount of space of memory which is consumed by the recursion is called a recursion stack space. For each recursive function, this space depends on the on the space needed by the local variable and the forma parameters. In addition, this space depends on the maximum depth of recursion i.e. maximum number of nested recursive calls.

In general, the total space needed by the program can be divided into two parts.

1. A fixed part that is independent of particular problems, and includes instruction space, space for constants, simple variables, and fixed sized structured variables.
2. A variable part that includes structured variable whose size depends on the particular problem being solved dynamically allocated space and he recursion stack space.

### 2. Time Complexity

The time complexity of an algorithm is the amount of time it needs to run a completion. In computer programming the time complexity any program or any code quantifies the amount of time taken by a program to run. The time complexity is define using some of notations like Big O notations, which excludes coefficients and lower order terms. The time complexity is said to be described asymptotically, when we describe it in this way i.e., as the input size goes to infinity. For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n for any n (bigger than some n0), the asymptotic time complexity is O(n3).

It is commonly calculated by calculating the number of instructions executed by the program or the algorithm, where an elementary operation takes a fixed amount of time to perform.

In general, the analysis of algorithm is achieved in two steps:

1. A prior analysis:

1. Presumes the assessment from temporal point of view of the used operations and their relative cost.
2. In a prior analysis, the result is a function which bound’s the algorithm’s computing time.

2. A posteriori testing supposes the following steps:

1. Establishing a convenient number of sets of input data, which presume the behavior possibilities of the algorithm.
2. Executing the algorithm for each input set and collecting actual stats about algorithm’s consumption of time and space while it is executing.
3. Build the algorithm’s profile the precise amount of time and storage the algorithm consumes.
4. A posteriori test has an objective to determine the algorithm’s profile.

TOP Interview Coding Problems/Challenges

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates