Unit 1
Unit 1
A[i]
Pseudo-Code
It is more structured than usual prose but
less formal than a programming language
Expressions:
use standard mathematical symbols to
describe numeric and boolean expressions
use for assignment (“=” in C)
use = for the equality relationship (“==” in
C)
Method Declarations:
Algorithm name(param1, param2)
Time and space complexity of
algorithm
• Analyzing an algorithm means determining the amount of resources (such as time and
memory) needed to execute it. Algorithms are generally designed to work with an
arbitrary number of inputs, so the efficiency or complexity of an algorithm is stated in
terms of time and space complexity.
• The time complexity of an algorithm is basically the running time of a program as a
function of the input size.
• The space complexity of an algorithm is the amount of computer memory that is
required during the program execution as a function of the input size.
Time space-Trade off
• The best algorithm to solve a particular problem at hand is no doubt the one that
requires less memory space and takes less time to complete its execution. But
practically, designing such an ideal algorithm is not a trivial task.
• There can be more than one algorithm to solve a particular problem. One may require
less memory space, while the other may require less CPU time to execute. Thus, it is
not uncommon to sacrifice one thing for the other.
• There exists a time–space trade-off among algorithms. So, if space is a big constraint,
then one might choose a program that takes less space at the cost of more CPU time.
On the contrary, if time is a major constraint, then one might choose a program that
takes minimum time to execute at the cost of more space.
Worst-case, Average-case,
Best-case
• Worst-case running time: This denotes the behaviour of an algorithm with respect to the
worst possible case of the input instance. The worst-case running time of an algorithm is an
upper bound on the running time for any input. Therefore, having the knowledge of worst-case
running time gives us an assurance that the algorithm will never go beyond this time limit.
• Average-case running time: The average-case running time of an algorithm is an estimate of
the running time for an ‘average’ input. It specifies the expected behaviour of the algorithm
when the input is randomly drawn from a given distribution. Average-case running time
assumes that all inputs of a given size are equally likely.
• Best-case running time: The term ‘best-case performance’ is used to analyse an algorithm
under optimal conditions. For example, the best case for a simple linear search on an array
occurs when the desired element is the first in the list. However, while developing and
choosing an algorithm to solve a problem, we hardly base our decision on the best-case
performance. It is always recommended to improve the average performance and the worst-
case performance of an algorithm.
Asymptotic analysis and
notations
• In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input
size (we don’t measure the actual running time). We calculate, how the time (or
space) taken by an algorithm increases with the input size.
• The main idea of asymptotic analysis is to have a measure of the efficiency of
algorithms that don’t depend on machine-specific constants and doesn’t require
algorithms to be implemented and time taken by programs to be compared.
Asymptotic notations are mathematical tools to represent the time complexity of
algorithms for asymptotic analysis. The following 3 asymptotic notations are mostly
used to represent the time complexity of algorithms.
BIG O Notation
Big O Notation: The Big O notation
defines an upper bound of an algorithm.