C++ Chapter 8 Searching and Sorting Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

A(n) __________ search uses a loop to sequentially step through an array. a. binary b. unary c. linear d. relative e. None of these

linear

A __________ algorithm is a method of locating a specific item of information in a larger collection of data. a. sort b. search c. standard d. linear e. None of these

search

A linear search can only be implemented with integer values.

False

Before you can perform a bubble sort, the data must be stored in descending order.

False

Before you can perform a selection sort, the data must be stored in ascending order.

False

The bubble sort is an easy way to arrange data in ascending order but it cannot arrange data in descending order.

False

The linear search repeatedly divides the portion of an array being searched in half.

False

You are more likely to find an item by using a binary search than by using a linear search.

False

The following function should swap the values contained in two integer variables, num1 and num2. What, if anything, is wrong with this function? void swap(int num1, int num2) { int temp = num2; num2 = num1; num1 = temp; } a. You must first initalize temp to 0 before using it. b. The variable temp should first be set to num1, not num2. c. The swap function must use reference parameters. d. The last line should be temp = num1. e. Nothing is wrong with this function.

The swap function must use reference parameters

The following is the pseudocode for which type of algorithm? For maxElement = each subscript in the array, from the last to the first For index = 0 To maxElement - 1 If array[index] > array[index + 1] swap array[index] with array[index + 1] End If End For End For a. bubble sort b. binary sort c. bubble search d. selection sort e. None of these

bubble sort

The __________ is adequate for searching through small arrays. a. binary search b. the linear search c. unary search d. bubble sort e. None of these

the linear search

On average, an item is just as likely to be found near the beginning of an array as near the end.

True

When an array is sorted from highest to lowest, it is said to be in a. reverse order b. forward order c. ascending order d. descending order e. None of these

descending order

The following is the pseudocode for which type of algorithm? Set found to false Set position to -1 Set index to 0 While found is false and index < number of elements If list[index] is equal to search value found = true position = index End If Add 1 to index End While Return position a. linear sort b. linear search c. binary search d. selection sort e. None of these

linear search

A binary search begins with the __________ element of an array. a. first b. last c. largest d. middle e. None of these

middle

The number of comparisons made by a binary search is expressed in powers of two.

True

Regardless of the algorithm being used, a search through an array is always performed a. from lowest to highest element b. from highest to lowest element c. beginning with the middle element d. using a binary search algorithm e. None of these

none of these

The following is the pseudocode for which type of algorithm? For start = each array subscript, from the first to the next-to-last minIndex = start minValue = array[start] For index = start + 1 To size - 1 If array[index] < minValue minValue = array[index] minIndex = index End If End For swap array[minIndex] with array[start] End For a. bubble sort b. binary sort c. bubble search d. selection sort e. None of these

selection sort

Using a linear search to find a value that is stored in the last element of an array that contains 20,000 elements, __________ elements must be compared. a. 20,000 b. only the first two c. only half d. 2,000 e. None of these

20,000

What is the output after the following code executes? int numerator = 5; int denominator = 25; int temp = 0; temp = numerator; numerator = denominator; denominator = temp; cout << numerator << "/" << denominator << " = " << (numerator/denominator) << endl; a. 5/25 = numerator/denominator b. 5/25 = 0 c. 5/25 = 0.2 d. 25/5 = 5 e. 25/5 = 25/5

25/5 = 5

A selection sort and a binary search can be applied to STL vectors as well as arrays.

True

If you are using the bubble sort algorithm to sort an array in descending order, the smaller values move toward the end.

True

The following is the pseudocode for which type of algorithm? Set first to 0 Set last to the last subscript in the array Set found to false Set position to -1 While found is not true and first is less than or equal to last Set middle to the subscript halfway between array[first] and array[last] If array[middle] equals the desired value Set found to true Set position to middle Else If array[middle] is greater than the desired value Set last to middle - 1 Else Set first to middle + 1 End If End While Return position a. linear sort b. linear search c. binary search d. selection sort e. None of these

binary search

A __________ search is more efficient than a __________ search. a. character, string b. integer, double c. binary, linear d. linear, binary e. None of these

binary, linear

Data that is to be sorted in ascending order is ordered a. from lowest value to highest value b. from highest value to lowest value c. with a binary search algorithm d. by identifying the middle value and going up and down from there e. None of these

from lowest to highest value

Assume you have two integer variables, num1 and num2. Which of the following is the correct way to swap the values in these two variables? a. int temp = num1; num2 = num1; num1 = num2; b. int temp = num2; num2 = num1; num1 = temp; c. num1 = num2; num2 = num1; d. int temp = num1; num2 = temp; temp = num2; num1 = temp; e. None of these

int temp = num2; num2 = num1; num1 = temp;

The __________ sort usually performs fewer exchanges than the __________ sort. a. bubble, selection b. binary, linear c. selection, bubble d. ANSI, ASCII e. None of these

selection, bubble

The advantage of a linear search is its a. complexity b. efficiency c. simplicity d. speed e. None of these

simplicity

Array elements must __________ before a binary search can be performed. a. summed b. set to zero c. positive integers d. sorted e. None of these

sorted

Algorithms used to arrange random data in some order are __________ algorithms. a. standard search b. sorting c. linear d. binary search e. None of these

sorting


Ensembles d'études connexes

Chapter 22 Introduction to Exercise Modalities

View Set

CRITICAL CARE Chapter 34- Patient Management- Nervous System

View Set

Top Mgmt and Political Skill Exam

View Set