Efficiency of Algorithm
Efficiency of Algorithm
DATA STRUCTURES
AND ALGORITHMS
2
• Analysis of algorithm:
• An investigation of an algorithm’s efficiency with respect
to two resources:
• Running time
• Memory space
3
Two kinds of efficiency
• Time efficiency: indicates how fast an algorithm
runs
• Space efficiency: deals with the extra space the
algorithm requires
4
Measuring an input’s size
• Algorithms efficiency can be investigated as a
function of some parameter n indicating the
algorithm’s input size
5
Units for measuring running time:
Time efficiency is analyzed by determining the number of repetitions of the
basic operation as a function of input size
• Basic operation: the operation that contributes the most towards the
running time of the algorithm
• Steps:
• Identify the most important operation of the algorithm, called basic
operation
• Compute the number times the basic operation is executed
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
or cost executed 6
Orders of Growth
7
Orders of Growth
8
Type Notation Example Algorithms
9
Worst case, best case and average case
efficiencies
10
Worst case, best case and average case
efficiencies
1. Worst-case efficiency of an algorithm is its
efficiency for the worst-case input of size n, which is
an input of size n for which the algorithm runs the
longest among all possible inputs of that size.
12
• Worst case:
• When there are no matching element S or the
first matching element happens to be the last
one on the list
• The algorithm makes the largest number of key
comparisons among all possible inputs of size n.
• Cworst(n) = n
• Best case:
• When the first matching element happens to be
the first one on the list
• The algorithm makes the smallest number of key
comparisons among all possible inputs of size n.
• Cbest(n) = 1
13
• Average case
• To analyze the algorithm’s average-case
efficiency, some assumptions about possible
inputs of size n has to be made
a) The probability of a successful search is p(0≤p ≤ 1)
14
Algorithm Analysis
• Algorithm analysis is the process of evaluating the performance
of an algorithm in terms of time and space complexity.
• The goal is to estimate the efficiency of an algorithm and
understand its behavior as the input size grows.
17
Big O vs. Big Theta vs. Big Omega
Big O: This represents the worst-case performance for an algorithm, setting
an upper bound on how slow your code can be. It’s noted as O(n²).
Big Theta (Θ): This represents the average-case, typical case performance
for an algorithm. It’s noted as Θ(n×p).
Big Omega (Ω): This represents the best-case performance for an
algorithm, setting a lower bound on how fast the code can perform. It’s
noted as Ω(n).
18
Analyzing Searching Algorithms
Linear Search:
• Time Complexity: O(n)
• Space Complexity: O(1)
Binary Search:
• Time Complexity: O(log n)
• Space Complexity: O(1)
19
Analyzing Sorting Algorithms
Big-O
• Helps estimate performance for large inputs.
• Guides the choice of algorithms in real-world scenarios.
• Example: Sorting a list of 1,000,000 elements—O(n log n) is preferable over
O(n2)
a. f(n) = 5n + 3
b. f(n) = 3n^2 + 7n + 2
c. f(n) = log(n^3)
23
Big O Notation - Problems
24
Big O Notation - Problems
25
Big O Notation - Problems
26
Thank You
27