270 likes | 344 Views
CompSci 105 SS 2005 Principles of Computer Science. Test Tomorrow. Lecture 16: Searching and Sorting (Thursday). Lecturer: Santokh Singh. Array Implementation. 0. 1. 2. 3. 4. 5. 6. 7. items:. 3. 5. 1. front:. 0. back:. 2. Textbook, pp. 306ff. “Circular Array”. 7. 0. 3.
E N D
CompSci 105 SS 2005 Principles of Computer Science Test Tomorrow. Lecture 16: Searching and Sorting (Thursday) Lecturer: Santokh Singh
Array Implementation 0 1 2 3 4 5 6 7 items: 3 5 1 front: 0 back: 2 Textbook, pp. 306ff
“Circular Array” 7 0 3 items: 6 1 5 front: 0 1 back: 2 5 2 4 3 Java Code, Textbook, pp. 310ff
Linked List Implementation 3 1 5 firstNode lastNode Textbook, pp. 302ff,
Circular Linked List 3 1 5 lastNode Java code, textbook, pp. 305ff,
ADT List Implementation • 1 • 5 • 3 Java Code, Textbook, pp. 263ff
Algorithm Efficiency Challenges in Measuring Efficiency Counting Operations Growth Rates of Functions Big O Notation Examples General Rules
Algorithm Efficiency Challenges in Measuring Efficiency Counting Operations Growth Rates of Functions Big O Notation Examples General Rules
Node curr = head; while (curr != null ) { System.out.println( curr.getItem() ); curr = curr.getNext(); } Textbook, p. 374
Rate of Growth x + ny time n
for ( i=1 to n ) { for ( j=1 to n ) { for ( k=1 to 5 ) { Task T } } }
Rate of Growth n2z time n
Rate of Growth n2z x + ny time n
Algorithm Efficiency Challenges in Measuring Efficiency Counting Operations Growth Rates of Functions Big O Notation Examples General Rules
Big O Notation “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” - Textbook, p. 376
Big O Notation “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” - Textbook, p. 376
Big O Notation “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” - Textbook, p. 376 Worst Case
Big O Notation “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” - Textbook, p. 376 Worst Case Large Problems
Rate of Growth n2z “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” x + ny time n Book Computes k and n0 exactly, p. 377
Rate of Growth n2z k n “An Algorithm A is order f(n) – denoted O(f(n)) – if constants k and n0 exist such that A requires no more than k * f(n) time units to solve a problem of size n≥n0” x + ny time n Book Computes k and n0 exactly, p. 377
for ( i=1 to n ) { for ( j=1 to n ) { for ( k=1 to 5 ) { Task T } } }
for ( i=1 to n ) { for ( j=1 to i ) { for ( k=1 to 5 ) { Task T } } } Textbook, p. 374-5, 377
O Notation Tricks • Ignore “low-order” terms • Ignore Constants • Can combine O()’s Textbook, p. 380
Comparing Growth Rates O(n2) Faster Growth Faster Code O(n) Textbook, p. 378
Comparing Growth Rates O(n2) Faster Growth Faster Code O(n) O(1) Textbook, p. 378
Comparing Growth Rates O(2n) O(n2) Faster Growth Faster Code O(n) O(log n) O(1) Textbook, p. 378
Comparing Growth Rates O(2n) O(n3) O(n2) Faster Growth O(n log n) Faster Code O(n) O(log n) O(1) Textbook, p. 378