

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































Some slides adopted from Princeton C0S226 course or Algorithms, 4th Edition
Tom Yeh he/him/his
ARRAYLIST add(Item item):Appends the item to the end of the ArrayList
Front End/Rear Capacity = 4 Number of items = 3
al.add(“!”); DOUBLE CAPACITY SINCE IT’S FULL AND THEN ADD NEW ITEM INCREASE NUMBER OF ITEMS What should happen? al.add(1, “THROWS”);
THEORY OF ALGORITHMS Asymptotic analysis simplifies analyzing worst-case performance ▸ Sorting them by increasing rate of growth: ▸ O (^1 ),^ O (log^ n ),^ O ( n ),^ O ( n^ log^ n ),^ O ( n
), O ( n
), O ( 2 n ), O ( n !)
THEORY OF ALGORITHMS How to interpret Big O ▸ or "order one”: running time does not change as size of the
▸ or "order log n”: running time increases as problem size grows.
▸ or "order n”: time increases proportionally to the the rate of growth
▸ or "order n squared”: Double the problem size you get quadruple
O ( 1 ) O (log n ) O ( n ) O ( n 2 )
RUNNING TIME OF ARRAYLIST OPERATIONS Worst-case performance of add() is O ( n ) ‣Cost model: 1 for insertion, for copying items to a new array. ‣Worst-case: If ArrayList is full, add() will need to call resize to create a new array of double the size, copy all items, insert new one. ‣Total cost:. ‣Realistically, this won’t be happening often and worst-case analysis can be too strict. We will use amortized time analysis instead. n n n + 1 = O ( n )
RUNNING TIME OF ARRAYLIST OPERATIONS Amortized analysis ‣Amortized cost per operation: for a sequence of operations, it is the total cost of operations divided by. ‣Simplest form of amortized analysis called aggregate method. More complicated methods exist, such as accounting (banking) and potential (physicist’s). n n
RUNNING TIME OF ARRAYLIST OPERATIONS Amortized analysis for n add() operations when increasing ArrayList by 1. ‣ (^) (“cost of insertions”). ‣ (^) (“cost of copying”) =. ‣ (^) total cost) , therefore amortized cost is or. ‣Same idea when increasing ArrayList size by a constant. ‣This is why in the lab yesterday, we saw that doubling was the fastest and linear(1) the slowest 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (^1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ) Insertion Cost 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Copying Cost 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Total Cost 17 ∑ = n ∑ 1 + 2 + 3 +... + n − 1 = n ( n − 1 )/ 2 O ( = n + n ( n − 1 )/ 2 = n ( n + 1 )/ 2 ( n + 1 )/ 2 O ( n )
ASSIGNED READINGS AND PRACTICE PROBLEMS Readings: ▸ Oracle’s guides: ▸ Collections:^ https://docs.oracle.com/javase/tutorial/collections/intro/index.html ▸ ArrayLists:^ https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html ▸ Textbook: ▸ Chapter 1.3 (Page 136–137) ▸ Chapter 1.4 (pages 197–199) ▸ Textbook Website: ▸ Resizable arrays:^ https://algs4.cs.princeton.edu/13stacks/ ▸ Analysis of Algorithms: https://algs4.cs.princeton.edu/14analysis/ Practice Problems: ▸ 1.4.1, 1.4.5 - 1.4.7, 1.4.32, 1.4.35-1.4.36.
TODAY’S LECTURE IN A NUTSHELL Lecture 7: Singly Linked Lists ▸ Singly Linked Lists Some slides adopted from Algorithms 4th Edition and Oracle tutorials
SINGLY LINKED LISTS Singly Linked Lists ‣ (^) Dynamic linear data structures. ‣ (^) In contrast to sequential data structures, linked data structures use pointers/links/references from one object to another.
Head/Beginning/Front/First
SINGLY LINKED LISTS Node private class Node { Item item; Node next; } item Node
SINGLY LINKED LISTS Standard Operations
linked list.
specified index.
specified index.
SINGLY LINKED LISTS SinglyLinkedList(): Constructs an empty SLL first = null n = 0 SinglyLinkedList
SINGLY LINKED LISTS add(Item item):Inserts the specified item at the head of the singly linked list sll.add(“CS062”) n= CS Head/Beginning/Front/First What should happen? sll.add(“ROCKS”);