# Lec3 - Note HW1 is a pencil-and-paper assignment due Friday...

• Notes
• 10

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

1 Lecture 3: Big O Notation PIC 10B Todd Wittman Note: HW1 is a pencil-and-paper assignment due Friday 11:00 at beginning of lecture. Ex : Searching A File square6 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 findPosition (ifstream& fin, int num) { int position = 1; int numberInFile; while (fin >> numberInFile) { if (numberInFile == num) return position; position++; } return -1; //Default code if num not found. }

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

2 Ex : Searching A File square6 Suppose there are N integers in the file. square6 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? square6 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) square6 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) square6 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 square6 The Big O gives tells us the dependence on the size of the data N. square6 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 square6 Big O is the asymptotic upper bound. It tells us the growth rate for large values of N. square6 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). square6 So we would say the average-case performance is O(N), pronounced “Big Oh of N” or “order N”. square6 What “N” is depends on the problem, usually the dataset size.
3 Ex : Vector Insertion square6 Last lecture, we looked at the vector insertion function. square6 Recall we had to shift all elements to right to make room. square6 The basic run time T depends on # elements we move. square6 Best-Case : If we insert at the end of the list, just do a push_back: T = O(1) square6 Worst-Case : If we insert at the beginning, have to move all N elements in the vector: T = O(N) square6 Average-Case : Inserting in the middle means we move N/2 elements: T = O(N) square6 Generally, we don’t care about the Best-Case performance.

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

This is the end of the preview. Sign up to access the rest of the document.
• Spring '08
• sgd
• Big O notation, Analysis of algorithms, Computational complexity theory, Run time, Subset sum problem

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern