Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views2 pages

Time and Space Complexity Beginner Guide

Uploaded by

praceenkumar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Time and Space Complexity Beginner Guide

Uploaded by

praceenkumar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Time and Space Complexity - Beginner's Guide

■ What is Time Complexity?


Time Complexity is the amount of time an algorithm takes to run as a function of the size of its input.
It tells us how fast or slow an algorithm performs.

For example:
If you are searching for an item in a list of 5 items, it might take 5 steps. If the list has 1000 items, it
might take 1000 steps. Time complexity helps us understand this growth pattern.

■ What is Space Complexity?


Space Complexity is the amount of memory an algorithm uses as a function of the size of its input.
It includes both the input size and any extra space (variables, data structures) used.

Example:
If your program uses a list of 10 numbers, and then also creates another temporary list inside,
space complexity will be based on both lists.

■ Why are TC and SC Important?


1 They help choose the most efficient algorithm.
2 They help write scalable code that works for large inputs.
3 They prevent slow apps and memory crashes.

■ How to Calculate Time Complexity (Step-by-Step)


1 1. Identify the basic operation (e.g., comparisons, additions).
2 2. Count how many times it runs in terms of 'n' (input size).
3 3. Ignore constants and less significant terms.
4 4. Use Big-O notation to express the result.

Example:
Code: for (int i = 0; i < n; i++) { print(i); } → Runs 'n' times → Time Complexity: O(n)

■ How to Calculate Space Complexity (Step-by-Step)


1 1. Count the memory used by variables and data structures.
2 2. Express it in terms of input size 'n'.
3 3. Ignore constants.

Example:
Code: int sum = 0; // uses constant space int[] arr = new int[n]; // uses n space → Space
Complexity: O(n)

■ Common Time Complexities


1 O(1): Constant Time - e.g., accessing an element
2 O(log n): Logarithmic Time - e.g., binary search
3 O(n): Linear Time - e.g., loop through array
4 O(n^2): Quadratic Time - e.g., nested loops

■ Comparing Two Approaches


Example 1: Linear Search for (int i = 0; i < n; i++) { if (arr[i] == key) return i; } → O(n) Example 2:
Binary Search while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == key) return mid; else if
(key < arr[mid]) high = mid - 1; else low = mid + 1; } → O(log n)

■ Conclusion
Understanding Time and Space Complexity is crucial for writing efficient code. It helps ensure your
program runs fast and uses memory wisely, especially when dealing with large inputs or real-world
applications.

You might also like