Sei sulla pagina 1di 11

26/01/2012 19:06:00

January 26, 12 Generics Its better to catch errors during compile time than run time. An array list of strings because its meant to contain strings-> cant cast from string to integer (in C you can just interpret it as an integer) In Java: Different arrays and create a print method to print all the arrays. Essentially the code is exactly the same. Overload to fit all forms of an array. o You can: public static void printarray(object [] a){}

February 2, 12 N^3 versus n Linear N log N Quadratic Cubic Cubic is better than Linear, N log N, Quadratic Basic cases (simple statements) constant time Assignment //O(c1) == O(1)

Break,continue, return //O(C2) ==O(1) System.in.println() //O(c3) ==O(1)

Structure Tree NODES ARE COMPEX STATEMENTS Public String int2bin(int n){ String rval; While (n>0){ If((n%2) ==0) o Rval =0 + rval; Else o Rval = 1 +rval; N=/2 } return rval; } Output: 5 -> 2_> 1 + -> 1 1-> 0 +1 == 01

0-> 1 + 01 ==101

O(1)

O(log base 2 n)

The address of a &a (x01b5) Int *p (declares a pointer to an integer) P = &a ( p is at the address of a) P[x01b5] in address (x0427) Int c = *p C[7] address (x219b) Function Calls In main we have int a and b A=3 B=5 A b

temp

3 (x321A) swap2 (px, py) px x321A (x7385)

5 (x321B)

py x321B (x7386)

temp 3 (x7387)

pointers equal arrays p=a same as p =&a[0] Linked List ( Self-referential data types) Class Node{ Private project Data;// the data Private Node next; //the link}So Im a node and I can point to a node Node is a reference to the next node

data next

ABE

BEA

CAL

Data Next

Data Next

Data Next

The list is like a bag and youre putting things in the bag. Has methods for insertion, deletion or lookup Public interface Dictionary{ Public void insert(Object ) Public void delete( Object ) Public void lookup(Object ) } public static void printList(Node front) { while(front !null) { system.out.println((front.data.toString()); front=front.next; }

public static void printList(Node front) { if(front ==null) return; else { system.out.println((front.data.toString()); printList(front.next); } } Height of a node n is the length of the longest path from n to a leaf Depth of a node is the length of the path form the root to n Height of the tree is the height of its root The children of any node ae often ordered from the left. Public void printinorder() If(lef!=null) Left.printinOrder

System element If right !=null Right.printInOrder();

Binary search tree at any node, every element in the left subtree are going to be less than that node. Running time = O(logn)(height of tree) 15 25 28 30 32 36 37 50 55 59 61 68 75 Now adding a parent to each node so we can keep track and make reference to it. Runtime depends on the tree O(n) linear tree If we order the tree differently, it can be quicker. Bushy trees AVL are binary search trees with additional property-> the height of the left and right subtree can differ but at most 1. SO mostly balanced. AVL theorem -> an avl tree of n elements has height (logn) Worst case: T(n) = T(n-1)(for recursive call) +n(for the comparisons in the partitions) worst case is O(n^2) Quicksort: nlogn 1. Pick a pivot 2. Reorder the list so that all elements with values less than the pivot come before the pivot while the elements with values greater than the pivot come after it. After this, the pivot is in its final position. 3. Recursively sort the sub-list of lesser elements and the sub-list of the greater element

26/01/2012 19:06:00

26/01/2012 19:06:00

26/01/2012 19:06:00

Potrebbero piacerti anche