cs lecture nov 15

cs lecture nov 15 - [-10][-7][2][11][14][38] void sort( int...

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

View Full Document Right Arrow Icon
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]
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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

This note was uploaded on 12/10/2011 for the course CS 137 taught by Professor Clarke during the Fall '10 term at Waterloo.

Page1 / 2

cs lecture nov 15 - [-10][-7][2][11][14][38] void sort( int...

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

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