cs lecture nov 15

# cs lecture nov 15 - -10-7[2[11[14[38 void sort int a int n...

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

cs lecture nov 15 quiz on big O November 25 t/f? 3x^2 + x -6 is O(x^2)? yes 4x^4 + 7x is O(x^3)? no 2n^2 -11 is O(n^2) yes 2^n + n10000 is O(2^n)? yes 3n^3 + 9 n^2 + 17 is O (n^4) yes, convention: try to give ‘tightest’ bound possible usually say 3n^2 + 9n^2 + 17 is O(n^2) (but O(n^2) <= O(n^3) <=O(n^4)..) sorting (e.g. an array of integers) void sort( int a[], int n); [38][-7][14][2][11][-10] in ascending order [-10][-7][2][11][14][38] -> ascending order selection sort -find smallest element -swap it into first position -repeat with rest of array [38][-7][14][2][11][-10] [-10][-7][14][2][11][38] [-10][-7][14][2][11][38] [-10][-7][2][14][11][38] [-10][-7][2][11][14][38]

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: [-10][-7][2][11][14][38] void sort( int a, int n){ int I,j; for(i=0;i<n-1;i++) for(j=i+1;<n;h++){ if(a[i]>a[j]){ int temp =a[i]; a[i]=a[j]; a[j]=temp; } } bubble sort int main(void) { int i; int a={38,-7,14,2,11,-10}; sort(a,6); for (i=0;i<6;i++) printf(“%d\n”,a[i])’ return 0; } time complexity outer loo: for(i=0;i<n=1;i++) overall time up to A(overhead)+B(n-1), increment & test + repeat body n-1 times inner loop for(j=i+1;j<n;j++) time C(overhead) + D ( n-(i+1))-> depends on total time (up to) A+B(n-1) + c(n-1) + D((n-1)+(n-2)+…1) =A + (B+C)(n-1) + D sum, I to n-1, i =A + (B+C)(n-1) + Dn(n^-1)/2 =O(n^2)...
View Full 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