Cosc 1437 Quiz 8 Review
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; }
The *swap* function must use reference parameters. Note: Use reference parameters when you need to change the value of the actual parameter variable in the call.
The number of comparisons made by a binary search is expressed in powers of two
True
The following is 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
Selection Sort
A binary search begins with the ________ element of an array.
Middle
Array elements must be _________ before a binary search can be performed.
Sorted
Using a linear search to find a value that is stored in the last element of an array of 20,000 elements, ____________ elements must be compared
20,000 Every element is searched, including the final value
A _________ search is more efficient than a _________ search.
Binary, linear
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) num1 = num2; num 2 = num 1; b) int temp = num1; num2 = num1; num1 = num2; c) int temp = num1; num2 = temp; temp = num2; num1 = temp; d) int temp = num2; num2 = num1; num1 = temp; e) None of the above.
D Answer choices A ,B, and C make the values of num1 and num2 the same, while storing the original num1 or num2 in temp.
A(n) _____ search uses a loop to sequentially step through an array.
Linear
This 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
Linear Search
When an array is sorted from highest to lowest, it is said to be in
descending order
The ________ is adequate for searching through small arrays.
linear search
The advantage of a linear search is its
simplicity
Algorithms used to arrange random data in some order are ________ algorithms.
sorting
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 <= last set middle to the subscript halfway between array[first] and array[last] If array[middle] = the desired value set found = true set position to middle else if array[middle] > desired value set last = (middle - 1) else set first = (middle + 1) end if end while return position
Binary Search
Before you can perform a bubble sort, the data must be stored in descending order.
False Bubble sort is used to sort data in ascending, descending, or alphabetical order. No prerequisites needed for bubble sort.
The linear search repeatedly divides the portion of an array being searched in half.
False Linear search looks at each element of the array to find the desired value
The bubble sort is an easy way to arrange data in ascending order, but it cannot arrange data in descending order
False More difficult way to arrange data. Can be done in ascending, descending, or alphabetical order
If you are using the bubble sort algorithm to sort an array in descending order, the smaller values move toward the end.
True Definition of descending order