# CS321B_week8 - CS32 Discussion Section 1B Week 8 TA Hao...

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

CS32 Discussion Section 1B Week 8 TA: Hao Yu (Cody)

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

View Full Document
Reminder & Agenda Time complexity Sorting algorithm Binary tree Project 3 due today 9:00 p.m. Homework 4 due next Tuesday 9:00 p.m.
Time Complexity Motivation How to evaluate the efficiency of a program? Elapsed time? Assume same input size and machine Program 1: Program 2: Truth Program 1: Program 2: We need an objective, quantitive approach! I am a worm~

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

View Full Document
Big-O Problem formulation Given an input of size n , how long does the algorithm take to finish the task in terms of n ? Basic concept: step calculation How many steps (#statement) does the algorithm take to finish the task in terms of n ? Example: 1 + 1 + 1 + n + n 3 + 2n 1 + 1 + n 2 2 + n 2
Big-O Calculation The upper- bound on the function’s growth The constant doesn’t matter O(n) = O(5n) = O(18n) != O(kn) Only the highest degree matters O(10n 3 + 7n 2 ) = O(8n 3 ) = O(2n 3 + nlogn) Tips Find the most time-consuming loop for-loop, while-loop, recursive Compute its total #statement

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

View Full Document
Big-O Calculation: Exercise 1 int sum ( int n) { int total = 0 ; for ( int i = 0 ; i<=n; i++) total += i; return total; } int sum2 ( int n) { int total = n*(n+ 1 )/ 2 ; return total; } n+1 O(n+1) = O(n) No loop! O(1)
Big-O Calculation: Exercise 2 void fun () { int m = 100 ; int n = 1000 ; for ( int i = 0 ; i<= m; i++){ for ( int j = 0 ; j <= n; j++) cout<< "Hello"<<endl; } } void all_pairs ( int arr[], int size) { for ( int i = 0 ; i < size; i++) { for ( int j = i; j< size; j++) { if (i != j) cout << i << " " << j << endl; } } } (m + 1) x (n + 1) O((m+1) x (n+1)) = O(mn + m + n + 1) = O(mn) Tip: O((m+1) x (n+1)) ≈ O(m x n) = O( mn) O(100 x 1000) = O(1) size x size O(size x size) = O(size 2 ) = O(n 2 )

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

View Full Document
Big-O Calculation: Exercise 3 int outputQ ( int n) { int counter = 0 ; for ( int i= 0 ; i < n ; i++ ) { for ( int j= 0 ; j < n ; j++ ) { counter++; cout << "Q"; break ; } } return counter; } n x n n x 1! O(n x 1) = O(n)
Big-O Calculation: Exercise 3 n x 10 O(n x 10) = O(n) int outputQ ( int n) { int counter = 0 ; for ( int i= 0 ; i < n ; i++ ) { for ( int j= 0 ; j < n ; j++ ) { counter++; cout << "Q"; if (j > 10 ) break ; } } return counter; }

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

View Full Document
Big-O Calculation: Exercise 3 1 + 1 + … + 1 + n + n + … + n 10 + (n - 10) x n 10n 2 100n O(10n 2 - 100n) = O(n 2 ) Tip: O(10 + (n- 10) x n) ≈ O(n x n) = O(n 2 ) int outputQ ( int n) { int counter = 0 ; for ( int i= 0 ; i < n ; i++ ) { for ( int j= 0 ; j < n ; j++ ) { counter++; cout << "Q"; if (i < 10 ) break ; } } return counter; }
This is the end of the preview. Sign up to access the rest of the document.

{[ 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