Algorithms Lec 3
Algorithms Lec 3
Algorithm Analysis
Fall 2024
Mr. Ahsan Shah
Algorithm Analysis
Algorithm Analysis
The time complexity of an algorithm using a The time complexity of an algorithm using a
posteriori analysis differ from system to system. priori analysis is same for every system.
If the time taken by the program is less, then the If the algorithm running faster, credit goes to the
credit will go to compiler and hardware. programmer.
Example
• let us consider the search problem (searching a given item) in a sorted array.
• Best case
• Worst case
• Average case
• Best case: Define the input for which algorithm takes less time or minimum time. In
the best case calculate the lower bound of an algorithm. Example: In the linear search
when search data is present at the first location of large data then the best case
occurs.
• Worst Case: Define the input for which algorithm takes a long time or maximum time.
In the worst calculate the upper bound of an algorithm. Example: In the linear search
when search data is not present at all then the worst case occurs.
• Average case: In the average case take all random inputs and calculate the
computation time for all inputs. And then we divide it by the total number of inputs.
Big O Notation
• Worst Case
• Upper Bound
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n
≥ n0 }
• Best Case
• Lower Bound
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n
≥ n0 }
• Average Case
• Exact Time
Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0 such that 0 ≤ c1g(n) ≤ f(n) ≤
c2g(n) for all n ≥ n0 }