CS Chapter 18

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

Consider the following list: int list [ ] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95} When performing a binary search, the target is compared first with _____. a. 4 b. 25 c. 39 d. 95

c. 39

T/F The sequential search algorithm does not require that the list be sorted.

True

In the case of an unsuccessful search, it can be shown that for a list of length n, a binary search makes approximately ________ key comparisons

2log2n 2logn

Let f and g be real-valued functions. Assume that f and g are nonnegative, that is, for all real numbers n, f(n) <= 0 and g(n) >= 0. We say that f(n) is ________ of g(n), written f(n) = O(g(n)), if there exist positive constants c and n0 such that f(n) <= cg(n) for all n >= n0

Big-O

T/F During the second iteration of a selection sort, the smallest item is moved to the top of the list

False

T/F If n = 1000, then to sort the list, selection sort, the smallest item is moved to the top of the list

False

T/F In a binary search, first, the search item is compared with the last element of the list

False

T/F In a bubble sort, the smaller elements move toward the bottom, and the larger elements move toward the top of the list.

False

T/F The swap function of quick sort is written differently from the swap function for selection sort.

False

To construct a search algorithm of the order less than log2n, it cannot be ______ based

comparison

In general, the selection sort algorithm is good only for small lists because _______ grows rapidly as n grows.

O(n^2)

Any sorting algorithm that sorts a list of n distinct elements by comparison of the keys only, in its worst case, makes at least ______ key comparisons.

O(nlog2n) O(nlogn)

T/F During the sorting phase of insertion sort, the array containing the list is divided into two sublists, sorted and unsorted

True

Assuming the following list declaration, which element is at the position 0 after the first iteration of selection sort? int list [ ] = {16, 30, 24, 7, 62, 45, 5, 55} a. 5 b, 7 c. 16 d. 62

a. 5

If n = 1000, to sort the list, bubble sort makes about ____ item assignments. a. 10,000 b. 100,000 c. 250,000 d. 500,000

c. 250,000

The behavior of quick sort is ___ in the worst case and ___ in the average case. a. O(nlog2n), O(nlog2n) b. O(n^2), O(n) c. O(nlog2n), O(n^2) d. O(n^2), O(nlog2n)

d. O(n^2), O(nlog2n)

For a list of length n, the bubble sort makes exactly ________ key comparisons

n(n-1)/2

In a quick sort, all of the sorting work is done by the function ________

partition

A sequence of branches in a comparison tree is called a(n) _______

path

The first step in the quick sort partition algorithm is to determine the _____ and swap it with the first element in the list.

pivot

T/F Suppose that L is a sorted list of size 1024, and we want to determine whether an item x is in L. From the binary search algorithm, it follows that every iteration of he while loop cuts the size of the search list by half

True

T/F The binary search algorithm can be written iteratively or recursively

True

T/F The selection sort algorithm finds the location of the smallest element in the unsorted portion of the list.

True

The behavior of merge sort is ___ in the worst case and ___ in the average case. a. O(nlog2n), O(nlog2n) b. O(n^2), O(n) c. O(nlog2n), O(n^2) d. O(n^2), O(nlog2n)

a. O(nlog2n), O(nlog2n)

____ sort requires knowing where the middle element of the list is. a. merge b. bubble c. insertion d. selection

a. merge

With the binary search algorithm, ____ key comparison(s) is/are made in the successful case---the last time through the loop. a. one b. two c. n-2 d. n

a. one

After the second iteration of bubble sort for a list of length n, the last ____ elements are sorted. a. two b. three c. n-2 d. n

a. two

Let f be a function of n. By the term ______, we mean the study of the function f as n becomes larger and larger without bond.

asymptotic

With insertion sort, the variable firstOutOfOrder is initialized to ____, assuming n is the length of the list. a. 0 b. 1 c. n - 1 d. n

b. 1

For a list of length n, selection sort makes ____ item assignments. a. n(n - 1)/2 b. 3(n - 1) c. 3(n) d. 4(n + 1)

b. 3(n - 1)

The sequential search algorithm uses a(n) ____ variable to track whether the item is found. a. int b. bool c. char d. double

b. bool

A sequential search of an n-element list takes ____ key comparisons on average to determine whether the search item is in the list. a. 0 b. n/2 c. n d. n^2

b. n

For a list of length n, insertion sort makes ___ key comparisons, in the worst case. a. n(n-1)/4 b. n(n-1)/2 c. n^2 d. n^3

b. n(n-1)/2

A comparison tree is a(n) __________ tree.

binary

The ______ search algorithm is the optimal worst-case algorithm for solving search problems by using the comparison menthod

binary

The following C++ function implements the ___sort algorithm. template <class elemType> void Sort (elemType list [ ], int length) { for (int iteration = 1; iteration < length; iteration++) { for (int index = 0; index < length - iteration; index++) { if (list [index] > list [index +1]) { elemType temp = list[index]; list [index] = list[index + 1]; list[ index + 1] = temp; } } } }

bubble

The formula to find the index of the middle element of a list is _________. a. (mid + last)/2 b.(first + last) - 2 c. (first + last) / 2 d. (first + mid) *2

c. (first + last) /2

Assume that list consists of the following elements. What is the result after bubble sort completes? int list [ ] = {2, 56, 34, 25, 73, 46, 89, 10, 5, 16}; a. 89 73 56 46 25 16 10 5 2 b. 2 56 34 25 5 16 89 46 73 c. 2 5 10 16 25 34 46 56 73 89 d. 2 10 16 25 34 46 56 73 89

c. 2 5 10 16 25 34 46 56 73 89

We can trace the execution of a comparison-based algorithm by using a graph called a _____. a. pivot table b. partition table c. comparison tree d. merge tree

c. comparison tree

Which of the following correctly states the quick sort algorithm? a. if (the list size is greater than 1) { a. Partition the list into four sublists. b. Quick sort sublist1. c. Quick sort sublist2. d. Quick sort sublist3. e. Quick sort sublist4. d. Combine the sorted lists. } b. a. Find the location of the smallest element. b. Move the smallest element to the beginning of the unsorted list c. if(the list size is greater than 1) { a. Partition the list into two sublists, say lowerSublist and upperSublist. b. Quick sort lowerSublist c. Quick sort upperSublist d. Combine the sorted lowerSublist and sorted upperSublist } d. if the list is of a size greater than 1 { a. Divide the list into two sublists b. Merge sort the first sublist c. Merge sort the second sublist. d. Merge the first sublist and the second sublist }

c. if(the list size is greater than 1) { a. Partition the list into two sublists, say lowerSublist and upperSublist. b. Quick sort lowerSublist c. Quick sort upperSublist d. Combine the sorted lowerSublist and sorted upperSublist }

In a bubble sort for list of length n, the first step is to compare elements _____. a. list [0] and list [n] b. list[0] and list [n-1] c. list [0] and list[1] d. list[n-1] and list[n+1]

c. list[0] and list[1]

When working with the unsorted portion of a list, the second step in a selection sort is to _____. a. divide the list into two parts b. move the smallest element to the top of the list (position 0) c. move the smallest element to the beginning of the unsorted list d. find the smallest element

c. move the smallest element to the beginning of the unsorted list

A sequential search of an n-element list takes ______ key comparisons if the item is not in the list. a. 0 b. n/2 c. n d. n^2

c. n

In the average case, sequential search typically searches ____. a. one quarter of the list b. one third of the list c. one half of the list d. the entire list

c. one half of the list

When moving array values for insertion sort, to move list[4] into list[2], first ____. a. move list [2] to list [3] b. delete list[2] c. move list [4] to list[3] d. copy list[4] into temp

d. copy list[4] into temp

If a list of eight elements is sorted using selection sort, the unsorted list is ____ after the second iteration. a. list[ 0 ] . . . list[ 1 ] b. list[ 0 ] . . . list[ 6 ] c. list[ 1 ] . . . list[ 7 ] d. list[ 2 ] . . . list[ 7]

d. list[ 2 ] . . . list[ 7]

Consider the following list: int list [ ] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95} When performing a binary search for 75, after the first comparison, the search is restricted to _____. a. list[0]...list[6] b. list[0]...list[7] c. list[5]...list[11] d. list[6]...list[11]

d. list[6]...list[11]

In the bubble sort algorithm, the following code accomplishes swapping values in elements at positions index and index + 1 a. list[index] = list[index + 1] list[index + 1] = list[index] b. list[index + 1] = list[index] list[index ] = list[index + 1] c. list[index] = temp; list[index ] = list[index + 1]; temp = list[index + 1]; d. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;

d. temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp;

The quick sort algorithm uses the _______ technique to sort a list.

divide and conquer

The top node of a comparison tree is call the ________ node.

root


Kaugnay na mga set ng pag-aaral

Ch. 6 Memory Strategies and Metacognition pt 2

View Set

PSI 15: life insurance policy provisions, options, & riders 10-12

View Set

ELA 974: Hazards and Risk: Introduction to Hazard Identification and Risk Analysis

View Set

quiz study for BIO 156IN thought question 2

View Set

Federal Tax Considerations for Life Insurance & Annuities

View Set

Chapter 21: Dermatologic Disorders

View Set

Study Guide Final Exam: Medical Office

View Set