notes20

notes20 - CS 251 - Lectures Last edited August 25, 2009...

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

View Full Document Right Arrow Icon
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 n0 such that n>n0 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 {
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 9

notes20 - CS 251 - Lectures Last edited August 25, 2009...

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

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