Big O Notation

Big O Notation - Lecture 3: Big O Notation PIC 10B Todd...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 3: Big O Notation PIC 10B Todd Wittman Ex : Searching A File s Yesterday in the PIC Lab you wrote a function to scan through a text file for the first occurrence of a target integer num. int position = 1; int numberInFile; while (fin >> numberInFile) { if (numberInFile == num) return position; position++; } return -1; //Default code if num not found. }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Ex : Searching A File s Suppose there are N integers in the file. s The basic running time T of the algorithm depends on how many numbers we had to check: (numberInFile == num) So what order is each run time in terms of N? s Best-Case : If the target num was at beginning of the file, only had to check 1 number. So T = 1. Run time is constant, doesn’t depend on N: O(1) s Worst-Case : If the target num was the last number in the file, we had to check all N numbers. So T = N. Run time is linear in N: O(N) s Average Case : For a random number, we had to check N/2 numbers on average. So T = N/2. Run time is also linear in N: O(N) Big O Notation s The Big O gives tells us the dependence on the size of the data N. s Def We say the running time T = O(f(N)) if there exists positive constants c and N 0 such that T c f(N) for all N N 0 s Big O is the asymptotic upper bound. It tells us the growth rate for large values of N. s For example, the average running time of our file scanning function was T = N/2. T = N/2 c N where c = 1/2. So T = O(N). s So we would say the average-case performance is O(N), pronounced “Big Oh of N” or “order N”. s What “N” is depends on the problem, usually the dataset size.
Background image of page 2
3 Ex : Vector Insertion s Last lecture, we looked at the vector insertion function. s Recall we had to shift all elements to right to make room. s The basic run time T depends on # elements we move. s Best-Case : If we insert at the end of the list, just do a push_back: T = O(1) s Worst-Case : If we insert at the beginning, have to move all N elements in the vector: T = O(N) s Average-Case : Inserting in the middle means we move N/2 elements: T = O(N) s Generally, we don’t care about the Best-Case performance. Ex
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 10

Big O Notation - Lecture 3: Big O Notation PIC 10B Todd...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online