Download presentation
Presentation is loading. Please wait.
1
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation
2
Objectives for Today Introduce Big-O notation Reading - K+W Chap 4.1-4.5, 2.6
3
Collection add(x) remove(x) member(x) size() first()
4
Single Linked List
5
Class Exercise Implement add, member, first
6
Efficiency of Algorithms An operation for our ADT can be thought of as a “problem” An algorithm solves the “problem” –A series of steps –Each step has a cost Time Space –Efficiency is a measurement of this cost
7
Example 2.14/2.16
8
Big-O notation Describes the relationship between input size and execution time If we double the number of inputs, n, and the execution time is approximately doubled –Linear growth rate –Growth rate has an order of n –O(n)
9
Example 2.14/2.16 Analysis 2.14 –execution time proportional to x_length –O(n) 2.16 –execution time proportional to (x_length) 2 –O(n 2 )
10
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25
11
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } n 2 executions
12
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } n executions, 5 statements per = 5n
13
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 1 execution, 25 statements = 25
14
Let’s count individual instructions for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Simple Statement } for (int k = 0; k < n; k++) { Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 } Simple Statement 1 Simple Statement 2... Simple Statement 25 T(n) = total execution time as a function of n T(n) = n 2 + 5n + 25
15
Formal Big-O Definition T(n) = n 2 + 5n + 25 T(n) = O(f(n)) means that there exists a function, f(n), that for sufficiently large n and some constant c: cf(n) T(n)
16
n 2 + 25n + 25 vs. 3n 2
17
Common Big-O Runtimes Constant -- O(1) Logarithmic -- O(log n) Fractional -- O(sqrt n) Linear -- O(n) Log-Linear-- O(n log n) Quadratic -- O(n 2 ) Cubic -- O(n 3 ) Exponential -- O(2 n ) Factorial -- O(n!)
18
Various Runtimes
19
Powers of n (Order of the polynomial)
20
Multiplicative Constants
21
Multiplicative Constants (cont’)
22
Dominant Terms (1)
23
Dominant Terms (2)
24
Dominant Terms (3)
25
Dominant Terms Comparison
26
Dominant Terms Comparison (cont’)
27
Class Exercise Implement last on our Linked List
28
Group Exercise Implement last in O(1)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.