ADSA Lecture 1
ADSA Lecture 1
Date: 13-08-2024
1. Identify the Basic Operations: Determine which operations are most significant in the
algorithm (e.g., comparisons, assignments).
2. Count the Operations: Calculate how many times the basic operations are performed as a
function of the input size.
3. Express Complexity Using Big O Notation: Find the dominant term and express it using Big O
notation.
7. Example Problems on Basic Time Complexity Analysis
1. Linear Search:
o Description: Searches for an element in an unsorted array by checking each element.
o Time Complexity: O(n), where n is the number of elements in the array.
o Explanation: In the worst case, each element must be checked once.
2. Binary Search:
o Description: Searches for an element in a sorted array by repeatedly dividing the search
interval in half.
o Time Complexity: O(logn).
o Explanation: Each step halves the search interval, leading to a logarithmic growth.
3. Bubble Sort:
o Description: Sorts an array by repeatedly stepping through the list, comparing adjacent
New Section 1 Page 2
o Description: Sorts an array by repeatedly stepping through the list, comparing adjacent
elements, and swapping them if they are in the wrong order.
o Time Complexity: O(n^2).
o Explanation: It involves nested loops, each running up to n times.
4. Merge Sort:
o Description: Sorts an array by dividing it into halves, sorting each half, and then merging the
sorted halves.
o Time Complexity: O(nlogn).
o Explanation: The array is divided log(n) times, and merging takes linear time.
These examples cover basic algorithms and their time complexities, providing a foundation for
understanding how different algorithms perform relative to the size of their input.
Time Complexity vs. Execution Time
1. Definition:
• Time Complexity:
o Concept: Time complexity is a theoretical measure of the time an algorithm takes to
run as a function of the input size n. It expresses the efficiency of an algorithm in
terms of the growth rate of its running time, focusing on how the time required by
the algorithm increases as the input size grows.
o Notation: Usually represented using Big O notation (e.g., O(n), O(logn), O(n^2), etc.).
o Purpose: Time complexity allows you to compare algorithms independently of the
hardware or software environment, providing a way to evaluate their scalability.
• Execution Time:
o Concept: Execution time is the actual time taken by an algorithm to run on a specific
machine with a specific input. It is the real-world measure of how long an algorithm
takes to execute, usually expressed in units of time like milliseconds, seconds, etc.
o Factors: Affected by various factors such as the processor speed, memory hierarchy,
compiler optimizations, and the specific input provided to the algorithm.
o Purpose: Execution time provides a concrete measure of an algorithm's performance
in a particular environment, allowing you to observe the actual time it takes to run a
specific piece of code.
2. Relationship Between Time Complexity and Execution Time:
• Abstraction vs. Reality:
o Time Complexity: Abstracts away hardware details and focuses on how the running
time of an algorithm scales with input size.
o Execution Time: Reflects the real performance of an algorithm on a specific system
with specific inputs.