INFM 370 FINAL EXAM

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Selection sort uses ~ ___________ compares and ~ ___________ exchanges to sort an array of length N.

(N^2)/2 compares , N exchanges

Given a randomly ordered array of length N with distinct keys, on average Insertion sort uses ~ ___________ compares and ~ ___________ exchanges.

(N^2)/4 compares, (N^2)/4 exchanges

Considering the computational complexity of sorting, when restricted to compare-based sorting algorithms, no algorithm can sort N items with fewer than _______________.

a linearithmic number of compares (i.e. T(N) ~ N lg N ).

An inversion in an array is a ...

pair of entries that are out of order.

For randomly ordered arrays of distinct values, the running times of insertion sort and selection sort are ____________________________ .

quadratic and within a small constant factor of one another

Consider the following in-place merge code. What is the purpose of this line? else if (j > hi ) a[k] = aux[i++];

right half exhausted (take from the left)

for( Node x = first; x != null; x = x.next ) { // Process }

Linked lists do not have indices like arrays and therefore cannot be traversed using the familiar for loop with an index i. What is the for loop that can be used with linked lists to traverse the items? Assume variable first points to the first node in the linked list.

For _____________ we do two recursive calls before working on the whole array.

Merge sort

Combining two ordered arrays to make one larger ordered array is known as _____________.

Merging

What is the best case compares and exchanges for Insertion sort?

N - 1 compares and 0 exchanges

The proof of the computational complexity of compare-based sorting relies on the fact that the compare tree has at least _______ leaves but no more than 2^h leaves.

N!

The worst-case number of compares for the Shellsort algorithm given in the book is proportional to _______.

N^(3/2)

What is a significant disadvantage of mergesort?

Not in-place

Item item = current.item; current = current.next; return item;

The ListIterator for linked list implementations of the Stack, Queue, and Bag is shown below. What is the correct code for the next() method?

What does the following formula calculate (i.e. what is H)? H = - ( p1 lg p1 +p2 lg p2 +. . . + pk lg pk )

The Shannon entropy of the keys

T or F? Insertion sort is stable

True

T or F? Merge sort is stable

True

T or F? Selection sort has minimal data movement, the number of exchanges is a linear function of the array size.

True

T or F? The standard Quick sort algorithm is in-place (i.e. uses less than c lg(N) extra memory).

True

uses space proportional to size

What is an advantage of the linked list data structure?

T(N) = a*N3

What is the equivalent of the following equation? lg( T(N) ) = 3*lg(N) + lg(a)

Generates a runtime exception.

What is the value of the following expression? 1/0

A NullPointerException on Line 5.

What will happen when the following code is run? 1. Counter[] rolls = new Counter[5]; 2. for( int i = 0; i < rolls.length; i++ ) 3. { 4. Counter c = rolls[i]; 5. c.increment(); 6. StdOut.println( c ); 7. }

Queue

Which fundamental data type has the following API (the class name has been replaced with Foo)? public class Foo<Item> implements Iterable<Item> Foo()-- create an empty Foo void enqueue(Item item) --add an item Item dequeue() --remove the least recently used item boolean isEmpty()-- is the Foo empty? int size() --number of items in the Foo

Stack

Which fundamental data type has the following API (the class name has been replaced with Foo)? public class Foo<Item> implements Iterable<Item> Foo()-- create an empty Foo void enqueue(Item item) --add an item Item pop()-- remove the most recently used item boolean isEmpty()-- is the Foo empty? int size() --number of items in the Foo

Bag

Which fundamental data type has the following API (the class name has been replaced with Foo)? public class Foo<Item> implements Iterable<Item> Foo() --create an empty Foo void add(Item item)-- add an item boolean isEmpty()-- is the Foo empty? int size() --number of items in the Foo

Queue<Transaction> collection = new Queue<Transaction>();(series of statements that add Transactions to collection) for( Transaction t : collection ) {StdOut.println(t);}

Which of the following represents the foreach construct?

Donald Knuth

Who postulated that, despite all of the complicating factors in understanding the running times of our programs, it is possible to build a mathematical model to describe the running time of any program?

b=2

You hypothesize the running time of a program to be as follows T(N) = a * Nb You have run experiments with N=8 and have measured T(N)=128 and a=2 . Solve for the variable b.

Which of the following variants to Quick sort are discussed for possible improvement?

1. Cutoff to insertion sort 2. Median of three partitioning

What heuristics are discussed for improving the running time of merge sort?

1. Eliminate the copy to the auxiliary array. 2. Test if the array is already in order. 3. Use Insertion sort for small subarrays.

In which cases would Insertion sort be appropriate?

1. For small arrays 2. For partially ordered arrays

In addition to implementing a total order, what other mathematical properties must the compareTo method adhere to?

1. Transitive (for all v, w, and x, if v <=w and w<=x then v <=x) 2. Reflexive (for all v, v= v) 3. Antisymmetric (for all v and w, if v<w then w>v and if v=w then w=v)

Counter heads = new Counter("heads");

Given the following API, how would you create a new object with id "heads"? public class Counter Counter(String id)-create a counter names id void increment() -- increment the counter by one int tally() -- number of increments since creation String toString() -- string representation

2 ones

Given the following code, what is printed out? Counter c1 = new Counter( "ones" ); c1.increment(); Counter c2 = c1; c2.increment(); StdOut.println(c1);

1

Given the following code, what is printed out? int x = 0; x++; int y = x; y++; StdOut.println( x );

A combinatorial property of binary trees is that a tree of height h has no more than ______ leaves.

2^h

aliasing

An assignment statement with a reference type creates a copy of the reference. The assignment statement does not create a new object, just another reference to an existing object. This situation is known as _________________

generics

An essential characteristic of collection ADTs is that we should be able to use them for any type of data. A specific Java mechanism known as _______________ enables this capability.

The following is the initial method for the Quicksort algorithm. What is the purpose of the shuffle? public static void sort(Comparable[] a) { StdRandom.shuffle(a); // What is the purpose of this? sort(a, 0, a.length - 1); }

Avoids worse case performance scenario.

Which merge sort implementation is the method of choice for sorting linked lists?

Bottom-up mergesort

No, however Java usually does the requisite type conversion automatically.

Can you directly compare a double to an int?

No. Those operations are defined only for primitive types.

Can you use < and > to compare String variables?

What integer should the compareTo() method return for the following cases? Case1: v < w Case2: v = w Case3: v > w

Case1: negative Case2: zero Case3: positive

The Sorting code works with any class that implements the ____________ interface

Comparable

When studying sorting algorithms, we count __________ and __________

Compares, exchanges

instance variables

Consider the following Anatomy Figure of the Counter abstract data type (this figure will be used for several questions). What is the term for A? private final String name; private int count;

Mergesort is one of the best-known examples of the utility of the ________________ paradigm for efficient algorithm design.

Divide-and-conquer

Quick sort with 3-way partitioning (similar to Dijkstra's National Flag problem) solves the problem of _______________________ .

Duplicate keys

constructors

Entries in an API with the same name as the class and lacking a return type are known as ______________

T or F? Selection sort is stable

False

T or F? Selection sort's running time is sensitive to the input.

False

T or F? Shell sort is stable

False

T or F? Shellsort requires a large amount of code.

False

T or F? Shellsort uses extra space proportional to the array size.

False

T or F? The standard Quick sort algorithm is stable.

False

boolean hasNext() Item next()

For a collection to be Iterable<Item> it must implement an iterator() method that returns an Iterator<Item> object. The Iterator<Item> must include the two methods

oldlast.next = last;

Given the Node class definition, what is the code to change from State 1 to State 2? oldlast -- last

the running time of the program, the input size

How long will a program take? A key observation for employing the scientific method to answer this question is to determine the relationship between _____________ and _______________ .

heads.increment()

How would you invoke (i.e. call) the instance method increment() on an object named heads?

Math.sqrt( 2.0 )

How would you invoke the static method sqrt(double v) on the Math class with a value of 2.0?

Sorting algorithms divide into two basic types: those that sort _____________ (use no extra memory except for perhaps a small function-call stack or a constant number of instance variables) and those that use an extra copy of the array to be sorted

In place

instance methods

In the Anatomy Figure, what is the term for C? public void increment() public int tally() public String toString()

class name

In the Anatomy Figure, what is the term for F? Public class Counter *Counter*

invoke constructor

In the Anatomy Figure, what is the term for H? Counter("tails");

invoke method

In the Anatomy Figure, what is the term for K? tails.tally()

Shellsort is a simple extension of ____________ sort that gains speed by allowing exchanges of array entries that are far apart, to produce partially sorted arrays.

Insertion

False

Java's String is a primitive type like int and double.

Consider the problem of finding the top M items in a large stream of N items. The sort client solution is to read all N items into memory, sort them, and then select the last M items. What is the order of growth of the time and space for this solution?

Order of growth Time: N log N Space: N

Consider the problem of finding the top M items in a large stream of N items. The PQ elementary client solution (ordered or unordered) is keep a list of the largest M items (e.g. in an array) and to compare each new N key against this list. What is the order of growth of the time and space for this solution?

Order of growth Time: NM Space: M

For _____________ we do two recursive calls after working on the whole array.

Quick sort

loitering

The condition of holding a reference to to an item that is no longer needed is known as _________________ .

What is the best case regarding the input data for Insertion sort?

The data is in sorted order.

type parameter

The notation <Item> after the class name in each of our fundamental data type APIs defines the name Item as a ______________ , a symbolic place-holder for some concrete type to be used by the client.

Iterator<String> i = collection.iterator(); while( i.hasNext() ) { String s = i.next(); StdOut.println(s); }

To conveniently support iteration the below foreach statement is shorthand for which of the following? Stack<String> collection = new Stack<String>();for( String s : collection ) {StdOut.println(s);}

-always uses space proportional to the size of the data -can be used for any type of data -has time per operation always independent of the size of the collection

The pushdown stack implementation using generics and a linked list

some push and pop operations require time proportional to the collection size.

The pushdown stack using an array with resizing still has the flaw that

-has time per operation always independent of the size of the collection -can be used for any type of data -always uses space proportional to the size of the data

The queue implementation using generics and a linked list

// Double the capacityresize( a.length * 2 );

The stack array implementation can be made to resize to avoid overflow during a push operation. What is the correct code?

// When only quarter occupied, half capacity if (N > 0 && N == a.length/4) resize(a.length/2);

The stack array implementation can be made to resize to avoid wasting memory during a pop operation. What is the correct code? public String pop(){ // Remove item from top of stack.String item = a[--N];a[N] = null; // Avoid loitering //### WHAT CODE SHOULD BE HERE ###// return item;}

index provides immediate access to any item

There are two fundamental data structures, arrays (also known as sequential allocation) and linked lists (also known as linked allocation). What is an advantage of the array data structure?

- works only for String objects - stack always uses space proportional to capacity (i.e. wastes memory) - risks overflow if collection grows larger than the array

What are the problems with the following strawman implementation of the Stack API?

takes input size and returns time (usually in milliseconds)

What does the function T(N) take as input and return?

A low level version of your program that runs on the Java virtual machine

What is Java bytecode?

need to know size on initialization

What is a disadvantage of the array data structure?

need reference to access an item

What is a disadvantage of the linked list data structure?

Consider the after scenario in Quick sort below, what can we say about the items in [lo , j)? Before: (lo) V |---------------- (hi) During: V | ---v---|(i)||||||||||(j)|----v---- After: (lo) ----v----||(V)||(j)||----v----(hi)

all the items are less than or equal the value at j

An in-place sorting algorithm uses memory <= _____________.

c lg(N), where c is a constant

Stirling's approximation indicates that the function ______________ can be approximated by ~ N lg (N).

lg(N!)

As a function of N, what is the height of a merge tree with 4 levels?

lg(N)

An array is said to be partially sorted if ...

the number of inversions is less than a constant multiple of the array size.

Stable sort means ...

to preserve the relative order of items with equal keys.

what are the two main priority queue operations?

void insert(Key v) key delMax()

Assuming distinct keys, if we remove the random shuffle, what is the worse case performance for Quick sort?

~ 1/2 N^2

Assuming distinct keys, Quick sort uses on average ___________ compares for N distinct keys.

~ 2 N lg N

Quick sort with 3-way partitioning's best case performance is ____________ .

~ N


Kaugnay na mga set ng pag-aaral

Essential Cosmic Perspective : Ch. 3 HW & Quiz

View Set

Violence In American Society Midterm

View Set

Corporate Finance Review Chapter 9

View Set