notes20

notes20 - CS 251 Lectures Last edited Analysis Tools Also...

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

CS 251 - Lectures Last edited August 25, 2009 Analysis Tools Also called the "big-oh" notation Definition - Given functions f(n) and g(n) we say that: f(n) is O(g(n)) if and only if f(n) <= Cg(n) for some n 0 such that n>n 0 and some C Asymptotic Notation Example Execution time (secs): f(n) = 0.000001n^2 + 100n + 1000 Execution time: O(n^2) When n-> Infinity we see that the term 0.000001n^2 becomes the most important. Therefore the execution time f(n) is O(n^2) The goal of asymptotic analysis is also to get rid of unnecessary information. It is expected that g(n) is O(g(n)) will be a small in order as possible Drop lower order factors and constants Special classes of algorithms o O(logn) logarithmic o O(n) linear o O(n^2) quadratic o O(n^k) polynomial o O(a^n) exponential this kind of problems are often called untractable some "Heuristic algorithms" are able to find solutions to these problems where the solution is a good solution but it is not optimal Example: Obtain maximum in an array of numbers. time = O(n) #include <assert.h> int maxInt(int a[], int n) { assert(n>0); // If n<=0, then program stops and prints "Assertion failed n>0" line number int max = a[0]; for(int i=1; i<n; i++) { if(max<a[i]) max = a[i]; } return max; } Example Sort an array of numbers time = O(n^2) void bubbleSort(int a[], int n) { for(int i=0; i<n-1; i++) { for(int j=0; j<n-i-1; j++) { if(a[j]>a[j+1]) { // swap int tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } Stacks A container of objects that are inserted and removed according to the Last-In-First-Out principle (LIFO) Two basic operations o push(o) inserts an item "o" to the top of the stack o pop(o) removes an item from the top of the stack Implementation: public class Stack {

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

View Full Document
private int capacity; private Object s[]; // array that implements stack private int top; // point to top of the stack (last element inserted public Array Stack(int capacity) { this.capacity = capacity; s = new Object[capacity]; top = -1; } public void push(Object o) { if(top == capacity) { // stack is full throw new stackFullException(); } // stack is not full top++; s[top] = o; } public Object pop() throws Stack Empty Exception { if(top == -1) { // stack is empty throw new StackEmptyException(); } // stack is not empty Object tmp = s[top]; top--; return tmp; } } Notes: This stack implementation stores elements of type Object. If you want to use it for primitive types like "int", you need to use a wrapper class like the Integer class o Stack stk = new Stack(10); o stk.push(new Integer(5)); o Integer i = (Integer) stk.pop(); o int val = i.value(); Uses of Stack: RPN Notation o It is a way to evaluate arithmetic expressions using a stack and that does not need parenthesis o Example: Normal Expression: 5*(2+3) RPN Notation: 5 2 3 + * Program execution o
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