Complexity Analysis
Complexity Analysis
ALGORITHMS
• STEP BY STEP PROCEDURE TO SOLVE A PROBLEM
• USED IN DAILY LIFE PROBLEMS
• IN CS, ALGORITHM IS DEFINED BY:
1- MUST HAVE SOME DATA TO OPERATE ON IT
2- MUST PRODUCE ATLEAST ONE RESULT
3- MUST TERMINATE AFTER FINITE NUMBER OF STEPS
• MANY ALGORITHMS HAVE BEEN DESIGNED
• Searching algorithms
• Sorting algorithms
• Compression algorithms
• Fast Fourier transforms( designed for digital signal processing)
• Encoding algorithms (used for encryption of data)
• Geometric algorithms( used for identification of geometric shapes)
• Pattern matching algorithms ( comparing images and shapes)
Classification of algorithms
• Depending on the strategy for solving a particular algorithms,
algorithms can be classified as:
• 1- Iterative (for example: sorting an array)
• 2 Divide and Conquer
• 3 Greedy algorithm ( an immediately available best solution at each
step is chosen) e.g: which way of conveyance to choose immediately
•4
Specification of algorithms
• Natural language
• Pseudo code
• Natural language: words like read, write, subtract, divide, repeat, for
while are used
• Pseudo code: It is a blend of natural language and programming
notation
Complexity
• Time complexity
• Space complexity
• Input
• Worst-case Complexity: Maximum time required for program
execution (Run slowest among all inputs) In the worst case analysis,
we calculate upper bound on running time of an algorithm.
• Average Complexity: Average time required for program execution.
Gives the necessary information about algorithm’s behavior on
random input.
• Best Case: Minimum time required for program execution (Run
Fastest among all inputs). It gives lower bound on running time of
algorithm for any instances of input.
• Why Worst Case Analysis?
The worst-case running time of an algorithm gives an upper bound on
the running time for any input. Knowing it provides a guarantee that
the algorithm will never take any longer.For some algorithms, the
worst case occurs fairly often. For example, in searching a database
for a particular piece of information, the searching algorithm’s worst
case will often occur when the information is not present in the
database.
• The “average case” is often roughly as bad as the worst case.
Method Worst case Average case
Selection sort N2 N2
Insertion sort N2 N2
Merge sort Nlogn Nlogn