Quiz 12
In the worst case, a linear search locates a value in an array of length n in ____ steps.
O(n)
If an element is present in an array of length n, how many element visits, on average, are necessary to find it using a linear search?
n / 2
The binarySearch method of the Collections class returns a value in the form of -k - 1 when the target item you are searching for was not found in the array. What does k represent?
It represents the index at which the target item should be inserted.
A portion of your program includes the method shown in the code snippet below to examine the elements of an array arr: private int findElement(int[] arr, int newVal) { int pos = Arrays.binarySearch(arr, newVal); return pos; } What can you conclude about the running time of this section of code?
Its running time will be O(log (n))
A portion of your program includes the loops shown in the code snippet below to examine the elements of two arrays, arr1 and arr2, both of length n: int matches = 0; for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { if (arr1[i].equals(arr2[j])) { matches++; } } } What can you conclude about the running time of this section of code?
Its running time will be O(n^2)
The method checkArray examines an array arr: public static boolean checkArray(int[] arr) { if (arr[0] >= arr[arr.length -1]) { return true; } return false; } What can you conclude about the running time of this section of code?
Its running time with be O(1)
The partial binary search method below is designed to search an array of String objects sorted in ascending order. Select the expression that would be needed to complete the method. public static int search(String[] a, int low, int high, String item) { if(low <= high) { int mid = (low + high) / 2; int result = ____________________________; if (result == 0) { return mid; } else if (result < 0) { return search(a, mid + 1, high, item); } else { return search(a, low, mid - 1, item); } } return -1; }
a[mid].compareTo(item)
Assume that bands is an ArrayList of Stringobjects, which contains a number of elements in ascending order. Select a statement to complete the code segment below, which invokes the Java library binarySearch method to search for the string "Beatles". If the list does not already contain the string, it should be inserted in an appropriate location so that the list remains sorted. int index = Collections.binarySearch(bands, "Beatles"); if (index < 0) { __________________________ }
bands.add(-1 - index, "Beatles");
The code segment below displays a table of numbers. Select an expression to complete the code segment, so that the resulting algorithm has O(n2) running time. for (int k = 1; k <=n; k++) { for _______________________ { System.out.print(j + ""); } System.out.println(); }
(int j = 0; j < k; j++)
The code segment below displays a pattern of asterisks. Select an expression to complete the code segment so that the resulting algorithm has O(n) running time. for (int k = 0; k < n; k++) { for _______________________ { System.out.print("*"); } System.out.println(); }
(int j = 1; j <= 10; j++)
Given the following code snippet for searching an array: int[] arr = {23, 25, 29, 34, 42}; int newVal = 15; int pos = Arrays.binarySearch(arr, newVal); What value will pos have when this code is executed?
-1
Suppose we are using binary search on an array with approximately 1,000,000 elements. How many visits should we expect to make in the worst case?
20
Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search?
4
Given an ordered array with 31 elements, how many elements must be visited in the worst case of binary search?
5
The sort method of the Arrays class sorts arrays containing objects of classes that implement the ____ interface.
Comparable
Which of the following arrays can be used in a call to the Arrays.sort method? I Any array with primitive numeric data, such as int, double, ... II Arrays of String or numeric wrapper classes like,Integer, Double, ... III Any class that implements the Comparableinterface
I, II, and III
If a call to the Arrays static method binarySearchreturns a value of 7, what can be concluded? I the element is not in the array II the element is at index 7 III the element occurs 7 times in the array
II
Suppose the call obj1.compareTo(obj2) returns 0. What can definitely be concluded from the return value? I obj1 and obj2 are the same object II obj1 and obj2 objects cannot be compared III the properties of obj1 and obj2 that are being compared have equal values
III
Binary search is an ____ algorithm.
O(log n)
Can you search the following array using binary search? int[] A = {6, 5, 4, 2, 0, 1, -1, -17};
No. Binary search can be applied to a sorted array only.
An algorithm that cuts the work in half in each step is an ____ algorithm.
O(log (n))
The analysis for the number of visits in a binary search begins with the equation, T(n) = T(n / 2) + 1. What does the number 1 represent in this equation?
One element visit before a recursive call on one half of the array.
Which of the following statements about running times of algorithms is correct?
When determining the running time, constants are not taken into consideration.
Suppose objects a and b are from a user-defined class that implements the Comparable interface. Which condition tests the compareTo method's return value to determine that a will precede bwhen the sort method is called?
a.compareTo(b) < 0
The partial linear search method below is designed to search an array of String objects. Select the expression that would be needed to complete the method. public static int search(String[] a, String item) { for(int i = 0; i < a.length; i++) { if ( ____________________________ ) { return i; } return -1; } }
a[I].equals(item)
A search technique where, in each step, you split the size of the search in half is called a____ search.
binary
A binary search is generally ____ a linear search.
faster than
Complete the code shown to define the Comparatorinterface for comparing Auto objects. public interface Comparator<Auto> { _____________________ }
int compare(Auto a, Auto b);
The following code is an example of a _____ search. public static int search(int[] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) { return i; } } return -1; }
linear
Suppose you wish to implement the Comparableinterface to allow your Vehicle class to compare Auto objects only. What is the correct way to do this?
public class Vehicle implements Comparable<Auto>
Complete the code shown to define the Comparatorinterface for comparing Auto objects. ___________________________ { int compare(Auto a, Auto b); }
public interface Comparator<Auto>
Another name for linear search is ____ search.
sequential