# 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:**

- Performance required i.e., time complexity.
- Memory requirement i.e., space complexity.
- 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**.

- A fixed part that is independent of particular problems, and includes instruction space, space for constants, simple variables, and fixed sized structured variables.
- 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 5n^{3} + 3n for any *n* (bigger than some n_{0}), the asymptotic time complexity is O(n^{3}).

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:**

- Presumes the assessment from temporal point of view of the used operations and their relative cost.
- 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:**

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

What's New

- C Language MCQs
- Python MCQs
- Perl MCQs
- MongoDB MCQs
- Java MCQs
- C# MCQs
- Scala MCQs
- Blockchain MCQs
- AutoCAD MCQs
- PHP MCQs
- JavaScript MCQs
- jQuery MCQs
- ReactJS MCQs
- AngularJS MCQs
- JSON MCQs
- Ajax MCQs
- SASS MCQs
- HTML MCQs
- Advanced CSS MCQs
- CSS MCQs
- OOPs MCQs
- PL/SQL MCQs
- SQL MCQs
- Oracle MCQs
- SQLite MCQs
- MS Word MCQs
- Software Engineering MCQs
- Operating System MCQs
- Data Analytics and Visualization MCQs
- MIS MCQs
- Linux MCQs
- WordPress MCQs
- Blogging MCQs

- Energy & Environment Engineering MCQs
- Project Management MCQs
- Marketing MCQs
- Generally Accepted Accounting Principles MCQs
- Bills of Exchange MCQs
- Business Environment MCQs
- Sustainable Development MCQs
- Marginal Costing and Absorption Costing MCQs
- Globalisation MCQs
- Indian Economy MCQs
- Retained Earnings MCQs
- Depreciation MCQs
- Partnership MCQs
- Sole Proprietorship MCQs
- Goods and Services Tax (GST) MCQs
- Cooperative Society MCQs
- Capital Market MCQs
- Business Studies MCQs
- Basic Accounting MCQs
- MIS Executive Interview Questions
- Go Language Interview Questions

Top Interview Coding Problems/Challenges!

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions!