Lec 09 Agenda 1/ Searching and sorting, logs (2:12) https://www.youtube.com/watch?v=1BIke9mprDw 2/Data structures Build an LinkedList. From this basic strcture, we can build most of the datastuctures we want. Except for binary trees. 3/ Game features. MovableAdapter,
Get source code labJava >git fetch origin lab09g >git checkout -b lab09g origin/lab09g
Arrays versus Collections Holds objects of known type. Fixed size. Collections Generalization of the array concept. Set of interfaces defined in Java for storing Objects. Multiple types of objects. Resizable.
Using Interfaces with Collections Interfaces are used for flexibility reasons Programs that use an interface are not coupled to a specific implementation of a collection. It is easy to change or replace the underlying collection class with another class that implements the same interface. Map map = new HashMap(); Map map = new TreeMap();
Searching Linear search O(n) --slow Binary search O(log2n) --fast Refresher on logs: If 23 = 8 then log28 = 3 Hashed search O(1) –fastest Search driver class (2:12) https://www.youtube.com/watch?v=1BIke9mprDw 5
6
Sorting SelectionSort O(n2) –-slow MergeSort O(n * log2n) –- fast HeapSort O(n * log2n) –- fast QuickSort O(n * log2n) –- fast 7
Applications of logs If you're playing in single-elimination (sudden- death) tournament with 16 players in your draw, how many matches would you need to win in order to win the tournament? Log216 You're searching a sorted set of 64 elements, what is the maximum number of times you would need to search this set? Log264 8
Applications of logs The radioactive half-life of Carbon-14 is 5730 years. You find a fossil whose Carbon-14 signature has a radioactive strength of 1/128 of the original strength. How old is this fossil? 9
Intro to Data Structures in Java Single Lined List Stack Binary Tree others...
http://people. cs. aau. dk/~torp/Teaching/E01/Oop/handouts/collections http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf 11
Iterator http://people.cs.aau.dk/~torp/Teaching/E01/Oop/handouts/collections.pdf 14