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

0% found this document useful (0 votes)
4 views3 pages

Basics Analysis On Algorithm

Uploaded by

RK
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)
4 views3 pages

Basics Analysis On Algorithm

Uploaded by

RK
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/ 3

Basics Analysis on Algorithm

Algorithm analysis involves determining the computational efficiency of an algorithm


in terms of time and space. Understanding the basics of algorithm analysis helps in
choosing the most suitable algorithm for a given problem and optimizing
performance.

Key Concepts in Algorithm Analysis

1.​ Time Complexity:


●​ Time complexity measures the amount of time an algorithm takes to
complete as a function of the input size. It’s usually expressed using
Big O notation, which provides an upper bound on the growth rate of
the running time.
2.​ Space Complexity:
●​ Space complexity measures the amount of memory an algorithm uses
as a function of the input size. Like time complexity, it is also
expressed using Big O notation.

Big O Notation

Big O notation describes the upper bound of an algorithm’s running time, giving the
worst-case scenario of its growth rate. Common Big O notations include:

1.​ O(1):
●​ Constant time: The algorithm’s running time is independent of the input
size.
●​ Example: Accessing an element in an array by index.
2.​ O(log n):
●​ Logarithmic time: The running time grows logarithmically with the input
size.
●​ Example: Binary search.
3.​ O(n):
●​ Linear time: The running time grows linearly with the input size.
●​ Example: Iterating through an array.
4.​ O(n log n):
●​ Linearithmic time: The running time grows in proportion to n log n.
●​ Example: Merge sort, quicksort (average case).
5.​ O(n^2):
●​ Quadratic time: The running time grows quadratically with the input
size.
●​ Example: Bubble sort, insertion sort.
6.​ O(2^n):
●​ Exponential time: The running time doubles with each additional
element in the input size.
●​ Example: Solving the traveling salesman problem with a brute-force
approach.
7.​ O(n!):
●​ Factorial time: The running time grows factorially with the input size.
●​ Example: Generating all permutations of a set.

Analyzing Time Complexity

1.​ Worst-Case Analysis:


●​ Determines the maximum time an algorithm will take for any input of
size n. It provides an upper bound and ensures the algorithm will never
take longer than this time.
2.​ Average-Case Analysis:
●​ Determines the expected time an algorithm will take for a random input
of size n. It provides a more realistic measure of performance for
typical cases.
3.​ Best-Case Analysis:
●​ Determines the minimum time an algorithm will take for any input of
size n. It is often less useful for practical purposes but can be
important for understanding the algorithm’s behavior in optimal
conditions.

Analyzing Space Complexity

1.​ Auxiliary Space:


●​ The extra space or temporary space used by an algorithm, not counting
the input size.
2.​ Total Space:
●​ The total space used by the algorithm, including the input size and any
auxiliary space.

Practical Considerations
1.​ Constants and Lower Order Terms:
●​ Big O notation ignores constants and lower order terms because they
become insignificant for large input sizes. However, for small inputs,
these factors can affect performance.
2.​ Amortized Analysis:
●​ Averages the time required to perform a sequence of operations,
ensuring that the average time per operation is small, even if some
operations may take longer than others.
3.​ Trade-offs:
●​ Sometimes, there is a trade-off between time and space complexity. An
algorithm that is faster may use more memory, and vice versa.
Understanding these trade-offs is crucial for optimizing performance.

Example Analysis

Linear Search

●​ Time Complexity:
●​ Best Case: O(1) (element found at the first position)
●​ Average Case: O(n) (element found somewhere in the middle)
●​ Worst Case: O(n) (element found at the last position or not found at all)
●​ Space Complexity:
●​ O(1) (no extra space required other than input)

Binary Search

●​ Time Complexity:
●​ Best Case: O(1) (element found at the middle position)
●​ Average Case: O(log n)
●​ Worst Case: O(log n)
●​ Space Complexity:
●​ O(1) (iterative version)
●​ O(log n) (recursive version due to call stack)

You might also like