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

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

View Full Document Right Arrow Icon
CS32 Discussion Section 1B Week 8 TA: Hao Yu (Cody)
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
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~
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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)
Image of page 6
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 )
Image of page 7

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

View Full Document Right Arrow Icon
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)
Image of page 8
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; }
Image of page 9

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

View Full Document Right Arrow Icon
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; }
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern