0% found this document useful (0 votes)
1 views

Comparing Algorithms

algorithms

Uploaded by

nadeemtalha072
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Comparing Algorithms

algorithms

Uploaded by

nadeemtalha072
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Comparing Algorithms

How Do You Compare Algorithms?

Test both algorithms by running them as programs.

Compare their use of resources (e.g., time and memory).

Challenges:

Writing two programs can be inconvenient.

A well-written program might perform better even if the algorithm isn’t


efficient.

Test cases might favor one algorithm unfairly.

Both algorithms might still fail to fit resource limits.

Asymptotic Analysis

What Is Asymptotic Analysis?

Measures how efficient an algorithm is as input size grows.

Helps decide if an algorithm is worth using.


Focuses mostly on running time and sometimes memory use.

Why Use Asymptotic Analysis?

It provides a clear picture of algorithm performance for large inputs.

Factors Affecting Running Time

Computer hardware: Speed of CPU, bus, and other components.

Network resources: Competing tasks might slow performance.

Programming language and compiler: Quality of the code generated.

Programmer skill: Efficient coding leads to better performance.

Estimating Algorithm Performance

How to Estimate Performance?

Count the number of basic steps an algorithm performs for input of size n.

Example: For sorting, count the number of comparisons made based on


the input size (number of items to sort).

What Are Basic Operations?

Actions like adding or comparing two numbers.

Example: Comparing two integers is a basic operation, but summing all


items in a list depends on the list size and isn’t basic.

Determining Running Time


Running time depends mostly on input size (n).

Express the time to run as a function of n.

Example:

If comparing two numbers takes one unit of time, then comparing n


numbers takes n units of time.

The total running time grows with the size of input n.

Algorithm Growth Rate

What Is Growth Rate?

Describes how much time or resources an algorithm needs as input size


increases.

Types of Growth Rates:

1. Linear Growth: Time grows in direct proportion to input size.

Example: If input doubles, time doubles.

2. Quadratic Growth: Time grows as the square of the input size.

Example: Doubling input makes time grow four times.

3. Logarithmic Growth: Time grows slowly even as input size increases.

4. Exponential Growth: Time grows extremely fast with input size.

Input Sizes and Cases

Input Examples:
1. Factorial Calculation: Requires just one number as input.

2. Finding the Largest Value: Searches every item in a list, no matter


where the largest value is.

Best, Worst, and Average Cases:

Best Case: Finds the target right away.

Worst Case: Finds the target after checking all items.

Average Case: Finds the target somewhere in the middle.

Faster Computer vs. Faster Algorithm

Sorting Example:

Insertion Sort is simple but slow for large inputs.

Merge Sort is more efficient for larger datasets.

Example:

Sorting 10 million numbers:

Insertion Sort might take days.

Merge Sort can do it in hours.

Why Choose Merge Sort?

As input size increases, Merge Sort becomes much faster than Insertion
Sort.

Even with a slower computer, a better algorithm (like Merge Sort) can
outperform.

Asymptotic Analysis in Detail

Studies algorithms when input size grows very large.

Helps simplify the understanding of time and resource use.


Upper Bounds (Big-O):

Shows the worst-case growth rate of an algorithm.

Example: Searching a list of size n takes at most O(n) time.

Lower Bounds (Big-Omega):

Shows the best-case performance of an algorithm.

Example: If searching takes at least 1 step, it’s Ω(1).

Tight Bounds (Theta):

Used when the best and worst cases match.

Example: If searching always takes n steps, it’s Θ(n).

Simplifying Analysis with Rules

1. Ignore Constants: Multiply constants don’t matter for big inputs.


Example: 5n simplifies to n.

2. Focus on Largest Term: Ignore lower-order terms. Example: n² + n


simplifies to n².

3. Add for Sequential Parts: Total cost is the sum of steps in sequence.

4. Multiply for Loops: Nested loops multiply their steps.

Calculating Running Times

For Loops:

Example: A loop running n times takes O(n).

Nested Loops:

Example: A loop inside another running n × n times takes O(n²).

While Loops:

Analyzed like for loops.

You might also like