StudySet CS445 term 2

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Insertion Sort

"Remove" the items one at a time from the original array, putting them into the correct relative sorted order as you insert.

Dividing Quicksort into steps

1) Instead of using index values to divide, break up the data based on how it compares to a special value, called a pivot value. 2)We don't need to use subproblem solutions to solve the overall problem, we don't need to do anything.

Why is the tower of Hanoi problem iteratively difficult to solve.

A recursive algorithm with a single recursive call still provides a linear chain of calls. When a recursive algorithm has 2 calls, the execution trace is now a binary tree, as we saw with the trace.

Insertion Sort on a linked list

More natural with a linked list - At each iteration simply remove the front node from the list, and "insert it in order" into a second, new list. In this case we are not creating ANY new nodes - just moving around what we have.

Comparison based sorting has a lower bound of NlgN. What does this statement mean? a) Any comparison based sorting algorithm has a run-time of at least NlgN. b) Any comparison based sorting algorithm has a run-time of at most NlgN. c) It is possible to write a program to sort data that takes NlgN time. d) It is not possible to write a program to sort data that takes NlgN time.

a) Any comparison based sorting algorithm has a run-time of at least NlgN.

Given our overall discussion of QuickSort and MergeSort, for which of the following data types might I prefer MergeSort to QuickSort? Select all that apply. a) Class People from the course handouts b) Class String c) long d) Class Worker from the course handouts

a) Class People from the course handouts d) Class Worker from the course handouts

We know that with the simple pivot (rightmost item) QuickSort, a worst case is when the data is sorted prior to the sort. If rather than the rightmost item for the pivot, we instead use the leftmost item as the pivot, how will the QuickSort run-time be affected?

a) It will make sorted data into a best case rather than a worst case b) Sorted data will still be a worst case for the algorithm c) Sorted data will not be the best case but it will no longer be the worst case

When we backtrack from call K to call K-1 in our 8-Queens solution, what do we do next? a) Remove the queen from its current row in column K-1 b) Make a new recursive call to column K+1 c) Backtrack to column K-2

a) Remove the queen from its current row in column K-1

Consider InsertionSort and the "sorted side" vs. the "unsorted side" of the array. What best describes the data on the "sorted side" of the array in InsertionSort?

a) The data is sorted in an absolute way -- each item at location k will stay at location k for the rest of the sort. b) The data is sorted in a relative way -- the data is sorted but it could still move as more items are "inserted". c) The data is not actually sorted at all -- that term is incorrect.

Which of the following are true about a solution to the 8-Queens problem? Indicate all true answers. a) There is exactly 1 queen in each column of the board b) There is exactly 1 queen in each row of the board c) There is exactly 1 queen in each diagonal of the board

a) There is exactly 1 queen in each column of the board b) There is exactly 1 queen in each row of the board

We know MergeSort has an asymptotic run-time of O(NlgN). This was informally determined by noting that the MergeSort execution tree has a) O(N) levels, each requiring O(lgN) work b) O(lgN) levels, each requiring O(N) work c) O(N) levels, each requiring O(N) work d) O(lgN) levels, each requiring O(lgN) work

b) O(lgN) levels, each requiring O(N) work

What does each recursive call represent in our 8-Queens problem solution? a) Placing a queen somewhere within a given row of the board b) Placing a queen somewhere within a given column of the board c) Placing all of the queens on the board

b) Placing a queen somewhere within a given column of the board

Logically, the "bins" that are used in RadixSort are a) Stacks b) Queues c) Bags d) Lists

b) Queues

If sorting an array of int in Java, which would you prefer ​[Note: To get credit for this problem you must indicate ALL correct answers] a) MergeSort because it is faster in actual run-time b) QuickSort because it is faster in actual run-time c) MergeSort because it is stable and QuickSort is not d) QuickSort because it is stable and MergeSort is not

b) QuickSort because it is faster in actual run-time

In our FindWord solution (to find Strings in a 2-d grid of characters), if we start at position (row, col) in the grid and we have been unsuccessful at finding the String in all directions, what does this mean? a) The String is not in the grid - there is no solution b) The String is not found starting at position (row, col) but could be somewhere else

b) The String is not found starting at position (row, col) but could be somewhere else

Consider InsertionSort of a linked list. What is the worst case scenario for this sort? a) The data is initially random. b) The data is initially sorted in order. c) The data is initially reverse sorted. d) All scenarios for this algorithm are the same.

b) The data is initially sorted in order.

The worst case situation for InsertionSort of an array occurs when a) The data is already sorted b) The data is reverse sorted c) The data is randomly distributed d) All cases for InsertionSort have the same behavior

b) The data is reverse sorted

RadixSort has a run-time of O(KN). What does K represent? a) The number of distinct values in the alphabet / character set b) The maximum number of characters / digits in any one key c) The total number of keys being sorted d) The number of Queues needed during the sorting process

b) The maximum number of characters / digits in any one key

Consider RadixSort and two arrays of N Strings. Array A has Strings with maximum length 5 and array B has Strings with maximum length 10. How would the actual run-times of RadixSort on array A and array B compare? a) The run-times for the two arrays should be approximately the same. b) The run-time for array B should be about a factor of 2 longer than the run-time for array A c) The run-time for array B should be about a factor of N longer than the run-time for array A

b) The run-time for array B should be about a factor of 2 longer than the run-time for array A

Consider the 8-Queens problem as discussed in lecture (with indices from 0 to 7). If a queen CANNOT be placed in row 6 of column k, the algorithm will a) Try to place a queen in row 5 of column k b) Try to place a queen in row 7 of column k c) Try to place a queen in row 0 of column k+1 d) Backtrack to column k-1 and resume in the next row there

b) Try to place a queen in row 7 of column k

When using the median of three partition algorithm, what is guaranteed in each partition? a) The data will be split evenly between the left and right sides b) The difference between the number of items on the left side and the number of items on the right side will be at most N/4 c) There will be at least one item on the left side and at least one item on the right side

c) There will be at least one item on the left side and at least one item on the right side

Consider ListIterator<T> interface. How could we efficiently implement this interface with a singly linked list? a) Have a first and a last reference in the ListIterator<T> object. b) Have a curr and a prev reference in the ListIterator<T> object c) We cannot efficiently implement ListIterator<T> with a singly linked list

c) We cannot efficiently implement ListIterator<T> with a singly linked list

In the findWord() method that was discussed, in a given call we store the current location, loc, that we are trying to match within the goal string (i.e. we want board​[r]​[c] == word.charAt(loc) to be true). When a call to findWord() backtracks, how do we update the value of loc to go back to its previous value (i.e. one index smaller)? a) We decrement loc so that it is assigned a smaller value (ex: loc = loc - 1). b) We have a list of loc values stored and access the previous one, which should have the previous value c) When the previous call resumes the value of loc in its activation record is the value that we want

c) When the previous call resumes the value of loc in its activation record is the value that we want

Consider the best case scenario for the QuickSort algorithm. In this scenario, how does each call to partition divide the data? Choose the best answer. a) N-1 items <= the pivot and 0 items greater than the pivot b) N-1 items >= the pivot and 0 items less than the pivot c) ~N/2 items <= the pivot and ~N/2 items >= the pivot d) Either A or B above

c) ~N/2 items <= the pivot and ~N/2 items >= the pivot

Consider the last iteration of the outer loop of InsertionSort. How many comparisons must be done for this iteration? a) No comparisons are required -- the last item is in its correct location by default. b) Only 1 comparison is needed -- it may have to swap with its neighbor but that is all. c) N-1 comparisons are needed -- it must be compared to all of the other items in the array d) It could be from 1 to N-1 comparisons, depending upon where the item will end up in the array.

d) It could be from 1 to N-1 comparisons, depending upon where the item will end up in the array.

steps to divide and conquer to sorting algorithms

1) How do we "divide" the problem into subproblems 2) How do we use the solutions of the subproblems to determine the overall soltuon?

Dual pivot Quicksort

- Use 2 pivots and create three partitions. This yields 3 subarrays that must be sorted recursively. As long as pivots are chosen wisely, this actually has an incremental improvement over traditional Quicksort.

mergesort vs quicksort?

- quicksort is likely faster in practice. - merge sort divides the set into the smallest possible groups immediately then reconstructs the incrementally as it sorts the groupings. - quicksort continually divides the set by the average, until the set is recursively sorted -the once concern with quicksort is that is unstable.

In the Towers of Hanoi solution, how many disks are actually moved in a single call to the method?

1.0

Consider an LList containing 5 items. We would like to "visit" all of the items in the list using calls to the getEntry(i) method. How many total nodes must be accessed in order to "visit" all of the items in this list?

15

Consider an array containing 100 items. If a random pivot is chosen for partition, what is the probability that the worst case for a single partition will occur (all items on one side or the other of the pivot)? Give your answer as a whole number to represent a percentage (ex: 10 would mean there is a 10% chance).

2

In the Towers of Hanoi solution with N = 5, how many total moves must be performed?

31.0

Towers of Hanoi Runtime

A move is a key instruction. Execution trace has exactly N levels. Each call of size X we have to do 1 move. This will be O(2^N)

How long does Merge Sort take to run?

At each level the number of problems doubles but size of each is cut in half. The total number of comparisons needed at each level stays the same - O(N). Since the size is cut in half with each call, we have a total of O(Nlog(base2)N) levels. In total we have N x log(base2)N work to do so our runtime is O(Nlog(base2)N)

Selection Sort

At iteration i of the outer loop, find the ith smallest item and swap it into location i.

Comparison based sorts.

Compares elements to other elements and moves if necessary.

Median of 3 quicksort

Consider front mid and back, median is pivot

How do we use subproblem solutions to solve the overall problem in Mergesort?

Consider one call, after both of its recursive calls have completed. We merge them together; this is where we really do work.

Downsides of Radix sort

Considerable overhead to the point for small or medium sized arrays K may be larger than lgN. Also not a generally applicable sorting algorithm.

Median of three Quicksort

Don't pick the pivot from any one index. Rather consider 3 possibilities each time we partition. Now sorted data is the best case, as opposed to the work case.

Finding words in 2-d grid of letters(findword.java)

Each recursive call will consider one location on the board. As recursive calls "stack up" we match letters in our board with characters in the word. One we match all characters we are done. We will try to backtrack if we get stuck within a word.

How long does QuickSort take to run if middle value in a partition.

Execution trace is similar to that of MergeSort, and the overall Big-O runtime is also O(Nlog(base2)N)

When would it be better to use merge sort?

For complex (object) types it may be better to use merge sort, as stability matters for objects.

How does insertion sort work at each iteration?

In each iteration of our outer loop, we will take an item out of the UNSORTED section and put it into its correct relative location in the SORTED section.

is Merge Sort better for linked list or is Quicksort better?

Mergesort works well but there is more overhead. Quicksort also could work, but would require a doubly linked list.

Does RadixSort sort in place? a) Yes b) No

No

Does median of three Quicksort guarantee that the worst case (N^2) will not occur?

No, but it reduces the likelihood of the worst case and makes the situation in which it would occur not obvious.

Bubble sort worst case

O(N^2)

Iterator

Program that allows us to iterate through a list in a sequential way

Shellsort

Rather than comparing adjacent items, we compare items that are farther away from each other. Specifically, we compare and "sort" items that are K locations apart for some K.

Why does Shellsort outperforms Insertionsort

Shellsort moves the data toward the best case of insertionsort. A good implementation of Shellsort will have O(N^3/2) execution

When to stop recursion for Quicksort?

Simple Quicksort stops when logical size is 1. Benefit of divide and conquer decreases as problem size gets smaller. So, it is better to choose a size > 1 to stop recursing and switch to another (good) algorithm at that point.

How do we "divide" the problem for MergeSort?

Simply break the array into half based on index value

Partition

Start with a counter on the left of the array and a counter on the right of the array. As long as data at left counter is less than the pivot do nothing. As long as the data at right counter is greater than the pivot do nothing.

What if we choose the plot index randomly?

The probability that it is an extreme item in the array? - 2/N What is the probability that the same thing occurs on the 2nd call - 2/(N-1). Proceeding in this manner we get (2^N)/(N!). Thus random pivot has a worst case of O(N^2) but the probability of this is infinitesimal and will usually be O(NlgN). generating random numbers reduces the performance too.

Why does the recursive solution for towers of Hanoi work.

The solution is recursive as it is solving the Hanoi problem with N-1 disks. The actual move of a disk is always done only 1 at a time.

Merge Sort and Towers of Hanoi similarities and differences

They both have tree execution. Towers of Hanoi required 2^n - 1 moves while MergeSort only requires O(NlgN) comparisons.

In the FindWord problem, if we succeed in finding a string at some position in the board, why might we still want to backtrack / continue the search?

To find more than one solution

Worst Case Insertion Sort Runtime

Unsorted array - we get (N-1)(N)/2 which is O(N^2)

How does the LL implementation use iterator

Uses a Node reference as the sole instance variable for the iterator

How do we implement the iterator

We Implement the iterator external to but "on top" of the list

How to recursively solve towers of Hanoi problem.

We can move N-1 disks from start to end. We can then move the last disk from start to end. We can then move N-1 disks from mid to end.

Selection Sort runtime

We don't do a comparion in the loop header - the iterations in both for loops are based solely on index value - total is (N-1)(N)/2 - same as InsertionSort worst case

8 Queens backtracking

We must undo a placement of a queen and try another one.

array list implementation with iterator

We need only an integer to store the index of the current value in iteration

Why is the 8 Queens problem difficult to do Iteratively.

We need to store state information as we try many locations on the board. With recursion the run time stack does this for us.

How do we ensure fair comparisons between words that differ in length

We pad at the right side with @

Worst Time for insertion sort

When data is already sorted - same runtime as worst case for array.

Consider the QuickSort algorithm as discussed in lecture. When are the data comparisons done in this algorithm? a) While the data is being "divided", prior to the recursive calls. b) While the data is being "put back together", after the recursive calls. c) Both before and after the recursive calls.

a) While the data is being "divided", prior to the recursive calls.

Consider ShellSort and specifically the last iteration of the outer loop. What is happening in this iteration of the outer loop? a) A K-Insertionsort is being done where K = N/2 b) A "full" InsertionSort is being done c) A SelectionSort is being done d) Nothing is done -- the data is sorted by this point

b) A "full" InsertionSort is being done

Consider some iterator, iter, which was created on a list of Integer. Consider the code below: while (true) { Integer X = iter.next(); System.out.println("Next value is " + X); } a) All of the values in the list will be printed, followed by an infinite number of null values b) All of the values in the list will be printed, followed by an Exception c) The code segment will immediately throw an Exception d) The code segment will not compile

b) All of the values in the list will be printed, followed by an Exception

When a backtracking algorithm "backtracks", what is happening in the execution? a) A new activation record is being pushed onto the run-time stack b) An activation record is being popped off of the run-time stack c) Execution continues using the same activation record d) A loop iterates

b) An activation record is being popped off of the run-time stack

Consider the getEntry(i) method in ListInterface, and a list containing N items. What is the worst case asymptotic run-time for getEntry(i) for an array list and for a linked list? a) Array list O(1), Linked list O(1) b) Array list O(1), Linked list O(N) c) Array list O(N), Linked list O(1) d) Array list O(N), Linked list O(N)

b) Array list O(1), Linked list O(N)

Given that the last iteration of the outer loop of ShellSort is a full InsertionSort, and given that there are several (many) iterations of the outer loop of ShellSort prior to the full InsertionSort, how does ShellSort have a better run-time than InsertionSort? a) The premise is incorrect -- ShellSort does not have a better run-time than InsertionSort. b) By the time ShellSort gets to the last iteration of the outer loop, the data is close to being sorted, which is the best case for InsertionSort. c) Magic.

b) By the time ShellSort gets to the last iteration of the outer loop, the data is close to being

In ShellSort, the last iteration of the outer loop a) Has nothing left to do since the data is sorted by this point b) Does a full InsertionSort of the data c) InsertionSorts the data that are N/2 locations apart in the array d) SelectionSorts the remaining items in the array

b) Does a full InsertionSort of the data

Consider the simple QuickSort algorithm discussed in lecture (rightmost element chosen as the pivot). Assume that the data is sorted prior to the call of QuickSort. Will this end up being a good or a bad case for the algorithm? a) It will yield best case behavior b) It will yield worst case behavior c) It will yield good but not best case behavior d) It will yield poor but not worst case behavior

b) It will yield worst case behavior

Consider the following code in the findWord method discussed in lecture: answer = findWord(r, c+1, word, loc+1, bo); if (!answer) answer = findWord(r+1, c, word, loc+1, bo); // more lines not shown Explain the purpose of the if statement prior to the second call of findWord() Select an answer and submit. For keyboard navigation, use the up/down arrow keys to select an answer. a) The second call will not typically be needed b) We only try the next direction if we did not succeed in the previous direction c) It is an error -- the "if" statement should not be there

b) We only try the next direction if we did not succeed in the previous direction

Consider the MergeSort algorithm as discussed in lecture. When are the data comparisons done in this algorithm? a) While the data is being "divided", prior to the recursive calls. b) While the data is being "put back together", after the recursive calls. c) Both before and after the recursive calls.

b) While the data is being "put back together", after the recursive calls.

Consider a list, L, containing (in order): 10, 20, 30Now consider the following code segment: Iterator<Integer> one = L.iterator(); Iterator<Integer> two = L.iterator(); Integer X = one.next(); Integer Y = two.next(); What are the values X and Y? a) X = 10, Y = 20 b) X = 10, Y = 10 c) X = 20, Y = 20

b) X = 10, Y = 10

Consider the MergeSort algorithm as discussed in lecture. At what point in the algorithm are the data actually compared to each other? a) As the data are being divided before the recursive calls b) In between the two recursive calls c) After both recursive calls have completed d) MergeSort is not a comparison-based sorting algorithm

c) After both recursive calls have completed

Consider standard Java List, L, of Integer containing 10, 20, 30 (in that order). What will be the effect of the code segment below? Iterator<Integer> one = L.iterator(); Iterator<Integer> two = L.iterator(); Integer X = one.next(); one.remove(); Integer Y = two.next(); a) X will be 10 and Y will be 10 b) X will be 10 and Y will be 20 c) An Exception will occur d) The code will not compile

c) An Exception will occur

Why might we use a base case other than 1 for QuickSort? a) A base case of 1 is actually invalid and we should never use that. b) As the problem sizes get smaller, the benefit of a divide and conquer approach increases. c) As the problem sizes get smaller, the benefit of a divide and conquer approach decreases.

c) As the problem sizes get smaller, the benefit of a divide and conquer approach decreases.

How does RadixSort handle Strings of different lengths? a) Nothing special needs to be done b) It "pads" shorter Strings on the LEFT with characters that come before 'A' in the ASCII set c) It "pads" shorter Strings on the RIGHT with characters that come before 'A' in the ASCII set

c) It "pads" shorter Strings on the RIGHT with characters that come before 'A' in the ASCII set

MergeSort does not sort in place and the author's code required a 2nd temporary array for the merge algorithm. How does this use of a temporary array affect the run-time of MergeSort? a) It degrades the asymptotic run-time of MergeSort and the empirical run-time of MergeSort b) It degrades the asymptotic run-time of MergeSort but it does not affect the empirical run-time of MergeSort c) It does not affect the asymptotic run-time of MergeSort but it degrades the empirical run-time of MergeSort d) It does not affect either the asymptotic run-time or the empirical run-time of MergeSort

c) It does not affect the asymptotic run-time of MergeSort but it degrades the empirical run-time of MergeSort

Consider SelectionSort and location N-1 in the array (where the array is indexed from 0 to N-1). How is the item that goes into this position "selected?" a) It is compared to the N-1 other items and put into its correct location. b) It is compared to its neighbor since that is the only item it could be switched with at this point in the algorithm c) It is not compared at all -- by the time we get to this item it is in the correct location by default.

c) It is not compared at all -- by the time we get to this item it is in the correct location by default.

Which of the following data scenarios CANNOT be the case after both recursive calls have completed in MergeSort (prior to actually merging), where we are sorting the data low to high? Indicate all scenarios that CANNOT be the case. a) Left: 10 30 50 70 Right: 20 40 60 80 b) Left: 10 20 30 40 Right: 50 60 70 80 c) Left: 10 30 20 40 Right: 50 70 60 80 d) Left: 50 60 70 80 Right: 10 20 30 40 e) Left: 40 30 20 10 Right: 80 70 60 50

c) Left: 10 30 20 40 Right: 50 70 60 80 e) Left: 40 30 20 10 Right: 80 70 60 50

What is our basic approach to implementing the Iterator<T> interface in the LList<T> class? a) Put the Iterator methods directly into the LList<T> class b) Make the Iterator implementation a separate public class that accesses the LList<T> class c) Make the Iterator implementation a private inner class within LList<T>

c) Make the Iterator implementation a private inner class within LList<T>

Assume you are in the last iteration of the outer loop for InsertionSort. Which statement below is correct? a) The last remaining item goes at the end of the array and no comparisons are needed b) The last remaining item must be moved all the way to the front of the array and N-1 comparisons are needed c) The last remaining item could end up in any position within the array.

c) The last remaining item could end up in any position within the array.

Consider the following Java code segment below, where L is a reference to some implementation of a standard Java List<Integer>. Iterator<Integer> iter = L.iterator(); while (iter.hasNext()) { Integer X = iter.next(); System.out.println("Next value is " + X); } Consider now two objects that may be stored in variable L: 1. An ArrayList<Integer> 2. A LinkedList<Integer>How will the run-time of the code differ with the two different objects? a) The ArrayList object will be asymptotically better than the LinkedList object b) The LinkedList object will be asymptotically better than the ArrayList object c) The run-times will be similar for both objects

c) The run-times will be similar for both objects

Which of the following statements is false? a) The Median of 3 version of QuickSort guarantees at least one value on each "side" of the pivot for each partition. b) The Median of 3 version of QuickSort will have the best case performance when the data is already sorted c) The Median of 3 version of QuickSort will have a worst case run-time of O(N^2) d) The Median of 3 version of QuickSort will have the worst case performance when the array is reverse sorted.

d) The Median of 3 version of QuickSort will have the worst case performance when the array is reverse sorted.

What is the worst case scenario for the initial state of the data in SelectionSort? a) The data is random b) The data is sorted in order c) The data is reverse sorted d) There is no worst case scenario -- all cases for SelectionSort require the same number of comparisons.

d) There is no worst case scenario -- all cases for SelectionSort require the same number of comparisons.

What does it mean that quicksort is not a stable sort?

if you are given two items A and B and A appears before B After Merge Sort A will be before B however this is not guaranteed with quicksort.

8 Queens Recursion

place 8 queens on the board by placing a queen in a legal (row, column) and recursively place 6 queens on the rest of the board.

backtracking

proceed forward until no solution can be found.

How long does QuickSort take to complete if pivot is always an extreme element

same O(N^2) runtime

We can define the Towers of Hanoi solution in the following way:To move N disks from start to end we Recursively move N-1 disks from _______ to _______ Move 1 disk from start to end Recursively move N - 1 disks from _______ to _______ For each blank, enter one of the values: start, middle, end.

start to middle middle to end

Average case for insertion sort

the comparisons are a little better, but still O(N^2)

Mode

the most frequently occurring score(s) in a distribution

How to make getEntry efficient with linked list

use iterator


संबंधित स्टडी सेट्स

Modern Database Management - Self Check 04

View Set

1,001 CCNA Questions Chapter 4: Introduction to Cisco IOS

View Set

Infotech English For Computer Users Work Book - Unit 22

View Set

Pharmacology Made Easy 4.0 The Cardiovascular System Test

View Set