0% found this document useful (0 votes)
92 views27 pages

L20-Efficiency of Algos PDF

This document discusses analyzing the efficiency of algorithms. It explains that algorithm analysis looks at both time and memory efficiency. The execution time of algorithms can be expressed in terms of input size using Big O notation, which provides an upper bound on growth rate. Common growth rates like O(1), O(log n), O(n), O(n log n), O(n^2), and O(2^n) are examined. The document contrasts worst-case and average-case analysis and provides examples analyzing sorting and searching algorithms.

Uploaded by

andrew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views27 pages

L20-Efficiency of Algos PDF

This document discusses analyzing the efficiency of algorithms. It explains that algorithm analysis looks at both time and memory efficiency. The execution time of algorithms can be expressed in terms of input size using Big O notation, which provides an upper bound on growth rate. Common growth rates like O(1), O(log n), O(n), O(n log n), O(n^2), and O(2^n) are examined. The document contrasts worst-case and average-case analysis and provides examples analyzing sorting and searching algorithms.

Uploaded by

andrew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CPSC 122 – Data Structures

Washington State University – Department of Computer Science


Efficiency of Algorithms
Analysis Of Algorithms

š Tools to see the differences between different algorithms

š Looks into specific differences of efficiency of the algorithms

š Look at different aspects of efficiency not just how long the code is

š The efficient use of both time and memory are the two most important aspects that we
look into
Time Efficiency

š How are the algorithms coded?


š Compare running time

š What computer should you use?


š Code efficiency should be independent of the type of computer being used

š What data should the programs use?


š Different data/use cases might give you different run times
Algorithm's Execution Time

š Related to the number of executions it requires


š Linked list versus array list

š Execution time is expressed in terms of the number n


š Number of items the algorithm needs to process
Analysis and Big O Notation

š Algorithm A: requires time proportional to f(n) or to be order of f(n)

š Order of f(n) - denoted O(f(n))

š f(n) - the algorithm's growth function

š Big O notation: notation using the letter O to denote order

š Example – problem of size n requires time directly proportional to n – problem is O(n)


Big O Notation Definition

Example : n^2-3n+10 O(n^2)

Note: Big O is can be seen as an


overestimation
Algorithm Growth Rate

š Algorithms time requirement is dependent on the problem size


š Number of nodes in the linked list

š Size of array

š Number of items in a stack

š Reach conclusions such as:


š Algorithm A: requires n^2/5 time units to solve a problem of size n

š Algorithm B: requires 5xn time units to solve a problem of size n


Algorithm Growth rate

š Further estimate time efficiency by stating:


• Algorithm A: requires time proportional to n^2
• Algorithm B: requires time proportional to n

š Even though you do not know the exact time it takes for Algo. A and Algo. B to complete
a specific sized problem

š You know that Algo. B takes less time than Algo. A for large problems
Growth Rates
Properties of growth-rate functions

Several mathematical properties of Big O notation help to simplify the analysis of an


algorithm.
1. You can ignore low-order terms in an algorithm’s growth-rate function.
§ O(n^3+4n^2+3n) O(n^3)

2. You can ignore a multiplicative constant in the high-order term of an algorithm’s growth-
rate function.
§ O(5n^3) O(n^3)

3. You can combine growth rate functions


§ O(f(n)) + O(g(n)) O(f(n)+g(n))
Example #1

Traversal of Linked List


Example #2

Nested For Loop


Solution
Example #3

For nested do_while


Solution
Worst Case and Average Case

š A particular algorithm might require different times to solve different problems of the same
size.

š The Worst Case: The maximum amount of time that an algorithm can require to solve a
problem of size n

š Shows that the algorithm is never slower than your estimate.


Sequential Search Algorithms

š Worst case: O(n)

š Average case: O(n/2) = O(n)

š Best case: O(1)


Binary search

š Big-O Analysis
š Unit of work: comparisons
š Best case
– target value is at first midpoint
– O(1) comparisons
š Worst case
– target value is not found
– list is cut in half until it is reduced to a list of size 0
– How many times can the list be cut in half? The number of times a number
n is divisible by another number m is defined to be the 𝑙𝑜𝑔𝑏 (𝑎), so the answer is
log2(𝑛) = 𝑶(lg 𝒏)
Analysis of Algorithms
Example:
Insertion Sort

š Set a marker after the first element


š Select the first unsorted element
š Swap other elements to the right to
create the correct position for the
unsorted element
š Advance to the next unsorted
element

You might also like