Download presentation
Presentation is loading. Please wait.
Published byGeorgina Ford Modified over 9 years ago
1
CS 146: Data Structures and Algorithms June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak
2
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 2 Primitive Types and Reference Types Java has primitive types and reference types. Primitive types are int, short, long, byte, float, double, char, and boolean.
3
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 3 Primitive Types and Reference Types, cont’d Reference types (AKA object types) are for objects that are created with the new operator. Java objects are always referred to by pointers. Object is the root of all reference types in the Java type hierarchy. Built-in reference types include Object, Integer, Float, Date, System, ArrayList, Hashtable An array is a reference type. You can define custom reference types.
4
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 4 More on Generic Types: Type Erasure Generic types like ArrayList are constructs of the Java language itself. The Java virtual machine does not know about generic types. The Java compiler does type erasure. Convert generic types back to their raw types. Replace type parameters by their bounds. Add type casting as necessary. There are restrictions on using generic types. Read about them in the text book, pp. 23-24.
5
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 5 Function Objects We saw how SimpleShape implements interface Comparable. Its compareTo() method compared areas. Suppose we want to write code that’s even more generic. We may want to use a variety of ways to compare shapes. Example: We want to compare rectangles’ heights. We can write define function objects that wrap each of the comparison methods.
6
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 6 Function Objects, cont’d Java interface Comparator in the java.util package declares a single method compare() that compares two objects passed in as parameters. The method returns a negative, zero, or positive int value depending on whether the first object is less than, equal to, or greater than the second object, respectively.
7
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 7 Function Objects, cont’d Example function object that implements the Comparator interface: private static class HeightComparator implements Comparator { public int compare(Rectangle rect1, Rectangle rect2) { return (int) (rect1.getHeight() - rect2.getHeight()); }
8
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 8 Array14: Use a Function Object public static void main(String[] args) { Rectangle rectangles[] = new Rectangle[] { new Rectangle(2, 5), new Rectangle(3, 4), new Rectangle(1, 6) }; System.out.println( "Maximum height: " + max(rectangles, new HeightComparator()).getHeight()); } Create a HeightComparator function object and pass it to the max() method.
9
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 9 Array14: Use a Function Object, cont’d private static MyType max(MyType elements[], Comparator comp) { int maxIndex = 0; for (int i = 1; i < elements.length; i++) { if (comp.compare(elements[i], elements[maxIndex]) > 0) { maxIndex = i; } return elements[maxIndex]; } Demo
10
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 10 Scalability of Different Algorithms LinearRuntimeGrowth: T(N) = O(N) LogarithmicRuntimeGrowth: T(N) = O(N log N) QuadraticRuntimeGrowth: T(N) = O(N 2 ) CubicRuntimeGrowth: T(N) = O(N 3 )
11
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 11 How to Say “ T(N) = O(f(N)) ” T(N) is Big-Oh of f(N) T(N) is order f(N) The = sign really doesn’t mean “equals” since there may be more than one function f(N) that satisfies T(N) = O(f(N)) So O(f(N)) is actually a set of functions. T(N) is in O(f(N))
12
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 12 Logarithms in Algorithm Analysis An algorithm is O(log N) if it takes a constant O(1) time to divide the problem, such as in half. Classic example: Binary search of a sorted array. An algorithm is O(N) if it takes a constant O(1) time to reduce the problem by a fixed amount, such as by 1. Classic example: Computing factorials.
13
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 13 Binary Search public > int binarySearch(AnyType elements[], AnyType x, boolean flag) { int low = 0; int high = elements.length - 1; int count = 0; while (low <= high) { int mid = (low + high)/2; int compare = x.compareTo(elements[mid]); ++count; if (compare < 0) { high = mid - 1; } else if (compare > 0) { low = mid + 1; } else { return flag ? mid : count; // found! } return flag ? NOT_FOUND : count; } Search earlier part. Search later part. Found! Return either the target index or the count, depending on the value of the boolean flag. BinarySearch.java
14
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 14 Binary Search, cont’d public static void main(String[] args) { int n = 10; BinarySearch searcher = new BinarySearch(); RandomGenerator generator = new RandomGenerator(n); Integer intArray[] = generator.generateSortedArray(n); for (int i = 0; i < n; i++) { System.out.printf("%2d:%2d\n", i, intArray[i]); } for (int i = 0; i <= 10; i++) { int target = generator.generateInt(); int index = searcher.binarySearch(intArray, target, true); System.out.printf("Search: %2d:%2d\n", index, target); } Demo Fill the array with random values from 0 through 9. Search for a random value from 0 through 9. Search1.java
15
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 15 Binary Search, cont’d private static final double LOG2 = Math.log(2); public static void main(String[] args) { System.out.printf("%8s%8s%15s\n", "n", "count", "log2(n)"); for (int n = 10; n <= 100000; n*=10) { BinarySearch searcher = new BinarySearch(); RandomGenerator generator = new RandomGenerator(100*n); Integer intArray[] = generator.generateSortedArray(n); int target = generator.generateInt(); int count = searcher.binarySearch(intArray, target, false); double log = Math.log(n)/LOG2; System.out.printf("%8d%8d%15f\n", n, count, log); } Demo Search2.java
16
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 16 Binary Search, cont’d n count log2(n) 10 4 3.321928 100 7 6.643856 1000 10 9.965784 10000 14 13.287712 100000 17 16.609640 1000000 20 19.931569 Therefore, for the binary search algorithm, what can we conclude about T ( n )? T(n) = Θ(log 2 n)
17
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 17 Abstract Data Type (ADT) A data type specifies: What kinds of data values belong in the type. What operations are permitted on those values. An abstract data type specifies: What kinds of data values belong in the type. What operations are permitted on those values. The implementations of the operations of an abstract data type are hidden. The implementations depend on the specific type.
18
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 18 Example ADT: List What kinds of data values? Determined by the implementing class. What operations? Implemented by the implementing class: Return the size of the list. Get the value at a given position of the list. Set the value at a given position of the list. Add a value to the end of the list (append). Add a value at a given position of the list (insert). Remove a value at a given position of the list. Iterate over the values in the list.
19
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 19 Example ADT: List, cont’d The ADT List can be implemented as: an array a linked list List is an interface: public interface List extends Collection { boolean add(AnyType element); void add(int index, AnyType element); AnyType get(int position); AnyType set(int index, AnyType element); ListIterator listIterator();... } Explained later.
20
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 20 ADT List as an ArrayList Advantages: Calls to get() and set() a value at a given position in the array are very fast (constant O(1) time). Disadvantages: Calls to add() a value (insert) or to remove() a value at a given position are expensive ( O(N) time), especially near the beginning of the array. Data values need to be shifted to make room for the new value (add) or to fill in the hole (remove). If the array capacity is reached, a stop-and-copy operation is required to increase the capacity.
21
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 21 ADT List as a LinkedList Advantages: Calls to add() and remove() at any given position in the list are very fast (constant O(1) time). The list capacity can grow (and shrink) at a constant O(1) time. Disadvantages: Accessing a value with get() or set() at a given position in the list is expensive ( O(N) time), especially near the middle of the list. Need to start at one end of the list and follow links to reach the desired node.
22
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak Break 22
23
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 23 Singly Linked List Each node of the list contains a next pointer that points to the next node in the list. Accessing the n th value in the list means starting at the head node and “chasing” links to the n th node. Adding or removing a given node is fast. Need to have access to the preceding node in order to modify the preceding node’s next pointer.
24
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 24 Singly Linked List, cont’d Insert a new node: Delete a node: Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7
25
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 25 Doubly Linked List Each node of the list contains a next pointer that points to the next node in the list and a prev pointer that points to the previous node. Accessing the n th value in the list means starting at the either the head node or the tail node, whichever is closer, and following the next or prev links, respectively, to the n th node. Adding or removing a node is fast.
26
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 26 Doubly Linked List, cont’d Use special head and tail nodes. Eliminate special cases in the code when inserting or deleting at the head or tail of the list. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7
27
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 27 Doubly Linked List, cont’d Insert a new node. Modify the pointers in the indicated order. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7
28
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 28 Doubly Linked List Delete a node. Modify the pointers in the indicated order. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7
29
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 29 Node Access Time: ArrayList vs. LinkedList private static ArrayList testArray; private static LinkedList testLinked; private static void initLists(int n) { testArray = new ArrayList<>(n); for (int i = 0; i < n; i++) { testArray.add(0); } testLinked = new LinkedList<>(testArray); } Access1.java Create an array list and a linked listboth containing elements with value 0.
30
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 30 Node Access Time with List.get() private static long timeElementAccess(List lst) { long start = System.currentTimeMillis(); for (int i = 0; i < lst.size(); i++) { Integer elmt = lst.get(i); } return System.currentTimeMillis() - start; } Access1.java Access each element successively.
31
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 31 Node Access Time with List.get(), cont’d public static void main(String args[]) { System.out.printf("%8s%15s%15s\n", "n", "ArrayList", "LinkedList"); for (int n = 100; n <= 1000000; n*=10) { initLists(n); long timeArray = timeElementAccess(testArray); long timeList = timeElementAccess(testLinked); System.out.printf("%8d%12d ms%12d ms\n", n, timeArray, timeList); } Demo Access1.java
32
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 32 Node Access Time with List.get(), cont’d ArrayList Access to each element is fast. LinkedList Access to each element is slow due to the need to chase links from either end of the list in order to reach the desired element. n ArrayList LinkedList 100 0 ms 0 ms 1000 0 ms 5 ms 10000 3 ms 43 ms 100000 0 ms 4208 ms 1000000 1 ms 821905 ms
33
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 33 The Java Collections Framework (JCF) A built-in collection of common data structures. Abstracted by the Collection interface: Extended by interface List. public interface Collection extends Iterable { boolean add(AnyType element); void clear(); boolean contains(AnyType element); boolean equals(AnyType element); boolean isEmpty(); boolean remove(AnyType element); Iterator iterator();... }
34
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 34 The Iterable Interface Allows you to use the special form of the for loop to iterate over the collection elements. An iterator keeps track of the current element. public interface Iterator { boolean hasNext(); AnyType next(); void remove(); } for (AnyType elmt : myElements) {... } No indexing or calls to get() required! Do something with elmt inside the loop.
35
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 35 Node Access Time with Iterator This time, we use the special form of the for loop to access the list elements sequentially. private static long timeElementAccess(List lst) { long start = System.currentTimeMillis(); for (Integer elmt : lst) { Integer elmt2 = elmt; } return System.currentTimeMillis() - start; } Access2.java Demo
36
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 36 Node Access Time with Iterator, cont’d n ArrayList LinkedList 100 1 0 1000 1 0 10000 5 3 100000 8 0 1000000 1 4 ArrayList Access to each element is fast. LinkedList Access to each element is fast.
37
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 37 Node Delete Time: ArrayList vs. LinkedList private static long timeElementRemove(List lst) { long start = System.currentTimeMillis(); int i = 0; while (i < lst.size()) { if (lst.get(i)%2 == 0) { lst.remove(i); } else { i++; } return System.currentTimeMillis() - start; } Remove1.java Use the List.remove() method. Demo
38
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 38 Node Delete Time with List.remove(), cont’d n ArrayList LinkedList 100 0 0 1000 0 6 10000 11 66 100000 401 6700 1000000 52605 1740173 ArrayList Access to each element is fast. Each deletion is slow due to the requirement to shift the remaining elements to fill the hole. LinkedList Access to each element is slow. Each deletion is fast.
39
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 39 Collection.remove() vs. Iterator.remove() The Collection interface and the Iterator interface each has a remove() method. Collection.remove() must first access the item. Iterator.remove() deletes its current item. You must call next() before calling remove() again. If you change the structure of a collection (e.g., by adding or removing items), the iterator becomes invalid. The iterator will throw ConcurrentModificationException if you try to use the iterator after changing the collection’s structure.
40
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 40 Node Delete Time with Iterator.remove() private static long timeElementRemove(List lst) { long start = System.currentTimeMillis(); Iterator iter = lst.iterator(); while (iter.hasNext()) { if (iter.next()%2 == 0) { iter.remove(); } return System.currentTimeMillis() - start; } Remove2.java Use the Iterator.remove() method. Demo
41
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 41 Node Delete Time with Iterator.remove(), cont’d n ArrayList LinkedList 100 1 0 1000 1 0 10000 13 6 100000 416 3 1000000 54656 17 ArrayList Access to each element is fast. Each deletion is slow due to the requirement to shift the remaining elements to fill the hole. LinkedList Access to each element is fast. Each deletion is fast.
42
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 42 So Now We Know That... ArrayList Access to a node at an arbitrary position is fast. Insertions and deletions are slow. LinkedList Access to a node at an arbitrary position is slow. Sequential node access using an iterator is fast. Insertions and deletions are fast.
43
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 43 Two Ways to Use an Iterator Via the special form of the for loop: An iterator object: for (Integer elmt : list) { // do something with elmt } Iterator iter = list.iterator(); while (iter.hasNext()) { Integer elmt = iter.next(); // do something with elmt }
44
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 44 The ListIterator Interface Created and returned by a list. The “cursor” marks the current position between nodes. Methods remove() and set() operate on the last node returned by next() or previous(). public Interface ListIterator extends Iterator { void add(AnyType element); boolean hasNext(); boolean hasPrevious(); AnyType next(); AnyType previous(); void remove(); void set(AnyType element);... }
45
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 45 The ListIterator Interface, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012 ISBN 978-0-13-257627-7
46
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 46 Example List Implementations Be sure to understand the example MyArrayList and MyLinkList implementations in the textbook. MyArrayList implements Iterable MyLinkedList implements Iterable
47
Computer Science Dept. Summer 2015: June 9 CS 146: Data Structures and Algorithms © R. Mak 47 Java Nested Classes Define a nested class inside its parent class: A nested class has access to all members of its parent class, even private members. A nested class is like a top-level (non-nested) class. It is nested in a top-level class for better packaging. public class ParentClass {... private static class NestedClass {... }... }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.