CS445
contiguous memory: PROS
(all elements are stored next to each other in memory) random access little per element memory overhead simple
Imagine you are writing a program where: you require a Bag data structure; it will run on a platform with very little memory; no other programs will be running at the same time; you always know how many elements will be stored in each Bag. Which implementation of the ADT Bag is more suitable for this program? A ArrayBag B LinkedBag
A ArrayBag
What is the best case runtime of binary search? A O(1) B O(n) C O(n^2) D O(log n)
A O(1)
What is the runtime of remove(E entry) in ArrayBag? A O(1) B O(n) C O(n^2) D O(log n)
B O(n)
What is the total runtime of executing a O(n) algorithm followed by another O(n) algorithm? A O(1) B O(n) C O(n^2) D O(n+1) E O(2n)
B O(n)
What was our high-level strategy for removing a specified entry from an array-based bag? A Remove and leave a null in its place B Swap it with the last element, then remove it from the end C Remove and shift later elements forward
B Swap it with the last element, then remove it from the end
Which element is the easiest to remove from a linked collection? A The last element B The first element C All elements are equally easy to remove
B The first element
Why does BagInterface declare the add method to return a boolean value, but our dynamic-capacity array implementation always returns true? A We made a mistake, and should modify the interface B The interface must permit any acceptable implementation, including fixed capacity C The implementation in incorrect, and should be modified to return false in failure D The add method will implicitly return false due to exceptions
B The interface must permit any acceptable implementation, including fixed capacity
When would you use amortized analysis? A When different inputs to an algorithm have different runtimes B When a sequence of actions includes predictable patterns C When you are likely to use a method multiple times D Any time normal algorithm analysis is too difficult
B When a sequence of actions includes predictable patterns
Given the following array elements, what does the array look like after two iterations of insertion sort, arranging items in ascending order? 24 10 7 31 12 3 22 A 3 10 7 31 12 24 22 B 3 7 10 31 12 24 22 C 7 10 24 31 12 3 22 D 10 24 7 31 12 3 22
C 7 10 24 31 12 3 22
Consider two algorithms that solve a problem, both with O(n) runtime. Which of the following can you conclude? A These algorithms will take the same amount of real-life time to run, if given the same input. B Regarding runtime, there is no reason to choose one of these algorithms over the other. C For large inputs, either of these algorithms is a better choice than a O(n * lg n) algorithm, but not as good as a O(lg n) algorithm. D If the input size is doubled, each of these algorithms will take 4 times as long.
C For large inputs, either of these algorithms is a better choice than a O(n * lg n) algorithm, but not as good as a O(lg n) algorithm.
Which of the following is a good application of stacks? A Storing music tracks from multiple users to play at a party B Storing assignments in a to-do list C Storing edits made to a document to enable undo D Storing news articles to read later
C Storing edits made to a document to enable undo
In an array-based implementation of Bag, the remove methods should replace references to removed entries with null because: A it is fail-safe programming practice B otherwise it could be considered a duplicate value C this allows the entry to be scheduled for garbage collection D the client expects a null return value
C this allows the entry to be scheduled for garbage collection
Which of the following is not part of an ADT? A What operations are supported B What data is stored C How a client can manipulate the stored data D How data is organized in memory
D
Which of the following lines of code is valid? A Car myRide = new Bicycle(); B Bicycle myRide = new Car(); C Car myRide = new MotorVehicle(); D MotorVehicle myRide = new Car();
D MotorVehicle myRide = new Car(); generic -> specific
ADT
Describes a data structure in an abstract language-independent way. what data / operations can client do?
Why do we use generics rather than just using the general Object type (with casts as necessary)?
Generics allow many type checks to happen at compile time
What methods are available to be called on an object of type B using a reference of interface type A (without casting)?
Only the methods declared in the interface A
Divide and Conquer
a problem-solving strategy in which a problem is broken down into sub-problems, until simple subproblems are reached.
contiguous memory: CONS
allocation must be done at once wastes memory or uses resizing( or both) requires shifting ( if order matter )
what is the fundamental difference between array implementations and linked implementations?
array implementations use contiguous memory
static
belongs to the class
Backtracking: main idea
brute force, build solution piece by piece look for errors
abstract class
can have abstract methods [no code] and cannot be instantiated
Backtracking: Next
change most recent decision to next option Returns null if there are no more options( alters no other decisions )
private
class
none
class, package
protected
class, package, subclass
public
class, package, subclass, else
Data structure that holds multiple objects of a particular common type
collection
Bubble sort
compare adjacent elements, swap relative order Best case O(n) Normal O(n2) horrible near best case behavior
interfaces
declares only abstract methods can declare static constants cannot be instantiated, define data or static members
Linked Bags
each element is independant in memory
(true / false) a reference cannot be of interface type because interfaces cannot be instantiated
false
(true / false) all entries in a bag must be of the same data type or a super type of that data type
false
(true / false) casting is used to change an objects type
false
(true / false) in an array-based implementation of a stack, it is more efficient to consider the first array location to the top of the stack
false
(true / false) in linked implementations of collections, implementing the node class as a static inner class allows it to be a non-generic class
false
(true / false) in order for recursion to be successful, all inputs must eventually lead to a recursive case
false
(true / false) the bottom item in a stack was the last item added
false
True or false?: Casting can change an object's type.
false, changes refrence
Selection sort
find smallest element among unsorted, and swap to position (sorted portion grows by 1) always O(n2)
recursion overheads (time)
generating activation records push / pop RTS garbage collecting
Backtracking: extend
going to the first branch. makes copy. returns null if there are no more decisions to make
Contains all of the Java code that makes the data structure work.
implementation
implements
implements code of generic methods an interface
extends
inherits all data members and instance methods from superclass
the complexity of an algorithm is expressed in term of its ___________
input size
Java representation of an ADT using abstract methods and comments
interface
nodes
keeps the element organized alongside a reference to the next element (current E, next Node)
recursion overheads (memory)
many activation records ( a lot of memory on RTS) RTS holds many
Bag structure =>
no limit on capacity unordered duplicates allowed
dynamic binding
object decides the code of be run a method call @ runtime
When using a stack to convert from postfix(weird) to infix(normal) notation, what is stored in the stack?
only operands ( numbers )
When using a stack to convert from infix(normal) to postfix(weird) notation, what is stored in the stack?
only operators ( * / + - )
Stacks
ordered, allow duplicates, add or remove from top ( last element added )
T extends Comparable <? super T>
rather than comparing T only with other T objects, this notation allows comparison to objects of a superclass of T
what determines method available? what code is executed?
reference type determines which methods are available. the object type determines what code is run
wildcards "?"
represents any class
Backtracking: Reject
returns true if it can be determined that the partial solution cannot be extended into a full solution
Backtracking: FullSolution
returns true if the partial solution is already a full solution to the problem
Insertion Sort
take first element in unsorted portion and SHIFT(no swap) into sorted position Best case O(n) Worst Case O(n2)
Instantiate
to call a constructor of a Class which creates an an instance or object, of the type of that Class. Instantiation allocates the initial memory for the object and returns a reference.
(true / false) In a linked collection, implementing the node class as a non-static inner class allows it to be a non-generic class.(Specific)
true
(true / false) a method can change the contents of an array passed to it as an argument
true
(true / false) a recursive algorithm will always be slower than an equivalent iterative version
true
(true / false) a variable can hold a reference to an objects type if the objects type is a subclass of the variable type
true
(true / false) in algorithm analysis, the general behavior of the algorithm is more important than the exact count of operations
true
super(.......)
us in subclass constructor to call superclass one
"final" keyword
used to specify that a method definition cannot be overridden with a new definition in a subclass
Shell sort
uses gap sequence that descends by 1 each iteration compares two elements the gap and inserts them back in relative order