APCS Chapter 14 Sorting

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

What is the smallest value of n for which n2 > 3n + 4? a) 6 b) 5 c) 4 d) 3

b

Another name for linear search is ____ search. a) sorted b) sequential c) random d) binary

b

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? a) Its running time will be O(n). b) Its running time will be O(n^2). c) Its running time will be O(log (n)). d) Its running time will be O(n log (n)).

c

An algorithm that cuts the work in half in each step is an ____ algorithm. a) O(n) b) O(n^2) c) O(log (n)) d) O(n log (n))

c

Find the simplest order of growth of the following expression: (n^3 + n + 3)^2. a) θ(n^5) b) θ(2^n) c) θ(n^6) d) θ(n^9)

c

Which sort algorithm starts by cutting the array in half and then recursively sorts each half? a) insertion sort b) selection sort c) merge sort d) quicksort

c

A binary search is generally ____ a linear search. a) slower than b) equal to c) less efficient than d) faster than

d

Suppose a developer gets class XYZ files and documentation from a subcontractor. This class does not implement the Comparable interface. What must be true in order for the developer to sort an array of XYZ objects without modifying the xyz class? a) The XYZ class must implement the Sortable interface. b) XYZ objects must be randomly distributed. c) XYZ objects must be ordered. d) The developer must supply a comparator object belonging to a class that implements the Comparator<XYZ> interface.

d

Suppose an algorithm requires a total of 3n3 + 2n2 - 3n + 4 visits. In big-Oh notation, the total number of visits is of what order? a) n^2 * n^2 b) n^2 c) n^6 d) n^3

d

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 identical values a) I b) II c) I and III d) III

d

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) a[low].compareTo(item) b) item.equals(a[mid]) c) item.compareTo(a[mid]) d) a[mid].compareTo(item)

d

The sort method of the Arrays class sorts arrays containing objects of classes that implement the ____ interface. a) Sortable b) Ordered c) Array d) Comparable

d

Which sort algorithm is used in the sort method in the Java Arrays class when the array length is less than 7? a) merge sort b) quicksort c) selection sort d) insertion sort

d

Merge sort has a O(n log2(n)) complexity. If a computer can sort 1,024 elements in an amount of time x, approximately how much longer will it take the computer to sort 1,024 times that many, or 1,048,576 elements? a) 1,024 times longer b) 2,048 times longer c) 8,192 times longer d) 1,048,576 times longer

c

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 b when the sort method is called? a) a.compareTo(b) == -1 b) a.compareTo(b) == 0 c) a.compareTo(b) < 0 d) a.compareTo(b) > 0

c

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? a) 1 b) 16 c) 20 d) 30

c

A portion of your program implements a loop in which each step contains a fixed number of actions. What can you conclude about the running time of this section of code? a) Its running time will be O(n). b) Its running time will be O(n2). c) Its running time will be O(log (n)). d) Its running time will be O(n log (n)).

a

A portion of your program includes the loop shown in the code snippet below to examine the elements of an array arr: int count = 0; int targetVal = 70; for (int i = 0; i < arr.length; i++) { if (arr[i] >= targetVal) { count++; } } What can you conclude about the running time of this section of code? a) Its running time will be O(n). b) Its running time will be O(n^2). c) Its running time will be O(log (n)). d) Its running time will be O(n log (n)).

a

After 9 iterations of selection sort working on an array of 10 elements, what must hold true? a) The largest element is correctly placed by default. b) One more iteration is needed to complete the sort. c) The smallest element is incorrectly placed. d) The largest element is incorrectly placed.

a

Assume we are using quicksort to sort an array in ascending order. What can we conclude about the indexes of two pivot elements placed in consecutive recursive calls? a) They are randomly located. b) They are in different halves of the array. c) They are both in the same half of the array. d) They are adjacent.

a

Choose the order of the following growth rates, from slowest to fastest: θ(n^3), θ(nlog(n)), θ(n^3/2), θ(2^n). a) θ(n log(n)), θ(n^3/2), θ(n^3), θ(2^n) b) θ(n^3), θ(n log(n)), θ(n^3/2), θ(2^n) c) θ(n^3/2), θ(n log(n)), θ(n^3), θ(2^n) d) θ(2^n), θ(n^3), θ(n^3/2), θ(n log(n))

a

Consider the following code snippet: public static void sort(int[] a) { for (int i = 1; i < a.length; i++) { int next = a[i]; int j = i; while (j > 0 && a[j - 1] > next) { a[j] = a[j - 1]; j--; } a[j] = next; } } What sort algorithm is used in this code? a) insertion sort b) selection sort c) merge sort d) quicksort

a

Consider the minimumPosition method from the SelectionSorter class. Complete the code to write a maximumPosition method that returns the index of the largest element in the range from index from to the end of the array. private static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) { if (a[i] < a[minPos]) { minPos = i; } } return minPos; } private static int maximumPosition(int[] a, int from) { int maxPos = from; for (int i = from + 1; i < a.length; i++) { ________________ } return maxPos; } a) if(a[i] > a[maxPos]) { maxPos = i; } b) if(a[i] == a[maxPos]) { maxPos = i; } c) if(a[i] < a[maxPos]) { maxPos = i; } d) if(a[i] <= a[maxPos]) { maxPos = i; }

a

Find the simplest order of growth of the following expression: n^3 + log(n^5). a) θ(n^3) b) θ(n^3+ log(n)) c) θ(log(n^5)) d) θ(n + log(n))

a

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? a) n / 2 b) n c) 2n d) n^2

a

If the array is already sorted, what is the performance of insertion sort? a) O(n) b) O(n^2) c) O(log n) d) O(n log n)

a

If you implement a recursive linear search, its performance will be ____. a) O(n) b) O(n^2) c) O(log (n)) d) O(n log (n))

a

In Big-Oh notation, selection sort is a(n) ____ algorithm. a) O(n^2) b) O(1) c) log n d) O(log n^2)

a

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? a) One element visit before a recursive call on one half of the array. b) The total number of recursions required. c) The fact that the number of elements is odd. d) The fact that we search either the left half or the right half, but not both.

a

The largestPosition method below returns the index of the largest element in the tail range of an array of integers. Select the expression that would be needed to complete the selectionSort method below, so that it sorts the elements in descending order. /** Finds the largest element in the tail range of an array. @param a the array to be searched @param from the first position in a to compare @return the position of the largest element in range a[from]..a[a.length - 1] */ private static int largestPosition(int[] a, int from) { int maxPos = from; for (int j = from + 1; j < a.length; j++) { if (a[j] > a[maxPos]) { maxPos = j; } } return maxPos; } public static void selectionSort(int[] a) { for ____________________________________ { int maxPos = largestPosition(a, i); ArrayUtil.swap(a, maxPos, i); } } a) (int i = 0; i < a.length - 1; i++) b) (int i = 0; i < a.length; i++) c) (int i = a.length; i > 0; i--) d) (int i = a.length - 1; i > 0; i--)

a

The method findLargest examines the elements of an array arr which contains non-negative values public static int findLargest(int[] arr) { int curLargest = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] >= curLargest) { curLargest = arr[i]; Maha Otaibi 99 } } return curLargest; } What can you conclude about the running time of this section of code? a) Its running time will be O(n). b) Its running time will be O(n^2). c) Its running time will be O(log (n)). d) Its running time will be O(n log (n)).

a

The performance of an algorithm is most closely related to what? a) The total number of element visits b) The total number of elements c) The type of elements d) The number of lines of code in the method

a

What type of algorithm places elements in order? a) sorting b) searching c) insertion d) deletion

a

Which selection sort iteration guarantees the array is sorted for a 10-element array? a) 9th iteration b) 10th iteration c) 1st iteration d) impossible to tell

a

Which sort algorithm starts with an initial sequence of size 1, which is assumed to be sorted, and increases the size of the sorted sequence in the array in each iteration? a) insertion sort b) selection sort c) merge sort d) quicksort

a

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? a) Its running time will be O(n). b) Its running time will be O(n^2). c) Its running time will be O(log (n)). d) Its running time will be O(n log (n)).

b

A search technique where, in each step, you split the size of the search in half is called a____ search. a) random b) binary c) linear d) merging

b

A version of which sort algorithm is used in the sort method in the Java Arrays class? a) merge sort b) quicksort c) selection sort d) insertion sort

b

After 5 iterations of selection sort working on an array of 10 elements, what must hold true? a) 5 more iterations are always necessary to complete the sort b) 4 more iterations are always necessary to complete the sort c) 5 more iterations may be needed to complete the sort d) 4 more iterations may be needed to complete the sort

b

After one iteration of selection sort working on an array of 10 elements, what must hold true? a) The array cannot be sorted. b) One element must be correctly placed. c) At least two elements are correctly placed. d) The largest element is correctly placed.

b

An algorithm that tests whether the first array element is equal to any of the other array elements would be an ____ algorithm. a) O(1) b) O(n) c) O(log (n)) d) O(n log (n))

b

Assume that names is an array of String objects that has been initialized with a large number of elements. Select the statement that would sort the elements in names in ascending alphabetic order. a) Arrays.sort(names, 0, names.length - 1); b) Arrays.sort(names); c) Collections.sort(names, 0, names.length - 1); d) Collections.sort(names);

b

Assume we are using quicksort to sort an array in ascending order. What can we conclude about the elements to the left of the currently placed pivot element? a) They are all sorted. b) They are all less than or equal to the pivot element. c) They are all greater than or equal to the pivot element. d) None can equal the pivot element.

b

Can you search the following array using binary search? int[] A = {6, 5, 4, 2, 0, 1, -1, -17}; a) Yes. Binary search can be applied to any array. b) No. Binary search can be applied to a sorted array only. c) Yes, but the algorithm runs slower because the array is in descending order. d) No, negative numbers are not allowed because they indicate that a value is not present.

b

Consider the sort method shown below for selection sort: public static void sort(int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the loop condition to read i < a.length. What would be the result? a) An exception would occur. b) The sort would work, but run one more iteration. c) The sort would work but with one less iteration. d) The sort would work exactly the same as before the code modification.

b

Consider the swap method shown below from the SelectionSorter class. If we modified it as shown in the swap2 method shown below, what would be the effect on the sort method? private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static void swap2(int[] a, int i, int j) { a[i] = a[j]; a[j] = a[i]; } a) There would be no effect. b) Some array elements would be overwritten. c) It would sort the array in reverse order. d) It would still be correct, but run a little faster.

b

Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search? a) 8 b) 4 c) 3 d) 2

b

Given the following code snippet for searching an array: int[] arr = {3, 8, 12, 14, 17}; int newVal = 15; int pos = Arrays.binarySearch(arr, newVal); What value will pos have when this code is executed? a) 5 b) -5 c) 6 d) -6

b

How many times can an array with 4,096 elements be cut into two equal pieces? a) 16 b) 12 c) 10 d) 8

b

If a call to the Arrays static method binarySearch returns 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 a) I b) II c) III d) II and III

b

If an element is present in an array of length n, how many element visits, in the worst case, are necessary to find it using a linear search? a) n / 2 b) n c) 2n d) n^2

b

If the Arrays static method binarySearch is called on an array of 10 elements and returns a value of 10, what can be concluded? I the element is not found II that value cannot be returned from method binarySearch III if added, the element would be the largest element in the array a) I b) II c) III d) I and III

b

If you want to use the Comparable interface, you must implement a single method called ____. a) comparable b) compareTo c) comparator d) compare

b

In each iteration, selection sort places which element in the correct location? a) The smallest in the array. b) The smallest element not yet placed in prior iterations. c) The largest element in the array. d) A random element.

b

Merge sort is a(n) ____ algorithm. a) O(n) b) O(n log(n)) c) O(n^2) d) O(log n)

b

Suppose an array has n elements. We visit element #1 one time, element #2 two times, element #3 three times, and so forth. How many total visits will there be? a) 2n b) n(n+1)/2 c) n^2 d) n^3

b

Suppose you wish to sort an array list of objects, but the object class does not implement the Comparable interface. Because you are not allowed to modify this class, you decide to provide a comparator object that implements the Comparator interface. Which method must you implement from this interface to achieve your objective? a) the sort method. b) the compare method. c) the compareTo method. d) the compareObject method.

b

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? a) It is the position of an existing item, and indicates that your target item should come after this existing item. b) It is the position of an existing item, and indicates that your target item should come before this existing item. c) It represents the number of times the array was accessed by the binarySearch method, and can be used for analyzing performance. d) Nothing - it is an arbitrary value.

b

The code segment below is designed to add the elements in an array of integers. Select the expression needed to complete the code segment so that it calculates the running time elapsed. long start = System.currentTimeMillis(); int sum = 0; for (int k = 0; k < values.length; k++) { sum = sum + values[k]; } long runningTime = ____________________________; Maha Otaibi 90 a) System.currentTimeMillis() b) System.currentTimeMillis() - start c) System.currentTimeMillis() + start d) runningTime + start - System.currentTimeMillis()

b

The code segment below prints some of the elements in an array with size n. Select an expression to complete the code segment so that the resulting algorithm has O(log n) running time. for __________________________ { System.out.println(array[j]); } a) (int j = 0; j < array.length; j = j + 2) b) (int j = 1; j < array.length; j = j * 2) c) (int j = 0; j < array.length / 2; j++) d) (int j = 0; j < array.length; j++)

b

What is the worst-case performance of insertion sort? a) O(n) b) O(n^2) c) O(log (n)) d) O(n log (n))

b

Which function has a faster growth rate: θ(n^1/2) or θ(log(n))? a) θ(log(n)) b) θ(n^1/2) c) They are the same. d) They can't be compared

b

Which of the following completes the selection sort method minimumPosition()? private static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) { ________________ } return minPos; } a) if (a[i] > a[minPos]) { minPos = i; } b) if (a[i] < a[minPos]) { minPos = i; } c) if (a[i] < a[j]) { minPos = i; } d) if (a[i] < a[minPos]) { i = minPos; }

b

Which of the following statements about running times of algorithms is correct? a) An algorithm that is O(1) means that only one comparison takes place. b) When determining the running time, constants are not taken into consideration. c) When determining the running time, lower-order terms must be taken into consideration. d) An algorithm that is O(n) means that the number of comparisons does not grow as the size of the array increases.

b

Which sort algorithm starts by partitioning the array and selecting a pivot element? a) merge sort b) quicksort c) selection sort d) insertion sort

b

Binary search is an ____ algorithm. a) O(n) b) O(n^2) c) O(log n) d) O(n log n)

c

Complete the following code for an interface that classes who wish to compare Auto objects can implement. public interface Comparator<Auto> { _____________________ } a) boolean compare(Auto a, Auto b); b) single compareTo(Auto a, Auto b); c) int compare(Auto a, Auto b); d) single compare(Auto a, Auto b);

c

Consider the sort method for selection sort shown below: public static void sort (int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the loop control to read int i = 1; i < a.length - 1; i++. What would be the result? a) An exception would occur b) The sort would not consider the last array element. c) The sort would not consider the first array element. d) The sort would still work correctly.

c

Given an ordered array with 31 elements, how many elements must be visited in the worst case of binary search? a) 16 b) 8 c) 5 d) 4

c

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? a) 0 b) 1 c) -1 d) -2

c

How large does n need to be so 0.3n2 is greater than 2.5n - 3? a) 3 b) 5 c) 7 d) 9

c

How many comparisons does selection sort make when sorting an array of length n? a) n b) log(2n) c) n( n + 1) / 2 d) n

c

How many times can an array with 729 elements be cut into three equal pieces? a) 4 b) 5 c) 6 d) 7

c

If you increase the size of a dataset fivefold, how much longer does it take to sort it with the selection sort algorithm? a) Approximately 5 times longer b) Approximately 20 times longer c) Approximately 25 times longer d) Approximately 100 times longer

c

In general, the expression ____ means that f grows no faster than g. a) f(n) = log g b) f(n) = log g^2 c) f(n) = O(g(n)) d) g(n) = O(f(n))

c

In the textbook, we determined that the merge method requires a total of 5n visits. We found that the number of visits required to sort an array of n elements is T(n) = T(n / 2) + T(n / 2) + 5n. What does T(n / 2) describe? a) the total number of visits to merge sort an array of n elements b) half the number of visits to merge sort an array of n elements c) the total number of visits to merge sort an array of n / 2 elements d) half the number of visits to merge sort an array of n / 2 elements

c

In the worst case, a linear search locates a value in an array of length n in ____ steps. a) O(n^2) b) O(log n) c) O(n) d) O(log n^2)

c

In the worst case, quicksort is a(n) ____ algorithm. a) O(n) b) O(log(n)) c) O(n^2) d) O(n log n)

c

Suppose you have a phone number and need to find the address that it corresponds to. If there are 2,000,000 entries, how many do you expect to search in a printed phone directory before finding the address you are looking for? a) Approximately 50,000 records. b) Approximately 75,000 records. c) Approximately 1,000,000 records. d) Approximately 1,200,000 records.

c

Suppose you wanted to test your sort on an array filled with different elements each time the code is run. What is an efficient technique for creating an array of 1,000 elements for each run? a) Run the program many times, entering different values for the array elements. b) Make a file with many sets of values and loop through them, sorting each one. c) Use the Random class to generate array elements, sorting each set in a loop. d) Create many different arrays with different elements in the program code and sort each array.

c

Suppose you wish to implement the Comparable interface to allow your Vehicle class to compare Auto objects only. Which of the following is the correct way to do this? a) public class Auto implements Comparable<Vehicle> b) public class Vehicle implements Comparable c) public class Vehicle implements Comparable<Auto> d) public class Vehicle implements Comparable(Auto)

c

The ____ class contains a sort method that can sort array lists. a) Sorting b) Arrays c) Collections d) Linear

c

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(); } a) (int j = n; j > 0; j = j / 2) b) (int j = 1; j < k; j = j * 2) c) (int j = 0; j < k; j++) d) (int j = 1; j <= 200; j++)

c

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; } a) sorted b) binary c) linear d) random

c

The merge sort algorithm presented in section 14.4, which sorts an array of integers in ascending order, uses the merge method which is partially shown below. Select the condition that would be needed to complete the method so that the elements are sorted in descending order. private static void merge(int[] first, int[] second, int[] a) { int iFirst = 0; int iSecond = 0; int j = 0; while (iFirst < first.length && iSecond < second.length) { if (_____________________________) { a[j] = first[iFirst]; iFirst++; } else { a[j] = second[iSecond]; iSecond++; } j++; } // rest of the method follows here } a) first[iFirst] < second[iSecond] b) iFirst < iSecond c) first[iFirst] > second[iSecond] d) iFirst > iSecond

c

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) a[i] == item b) a[i].compareTo(item) c) a[i].equals(item) d) a[i].indexOf(item)

c

When the size of an array increases by a factor of 100, the time required by selection sort increases by a factor of ____. a) 2,000 b) 5,000 c) 10,000 d) 12,000

c

Which of the following classes implement the Comparable interface? I Date II Collections III String a) I b) I and II c) I and III d) II and III

c

Which of the sorts in the textbook can be characterized by the fact that even in the worst case the running time will be O(n log(n)))? I quicksort II selection sort III merge sort a) I b) II c) III d) I and III

c

Which of the sorts in the textbook can be characterized by the fact that the best case will have a running time of θ(n) if the data is already sorted? I quicksort II selection sort III insertion sort a) I b) II c) III d) I and III

c

Assume that bands is an ArrayList of String objects 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) { __________________________ Maha Otaibi 101 } a) bands.add(-1 * index + 1, "Beatles"); b) bands.add(index + 1, "Beatles"); c) bands.add(-1 * index, "Beatles"); d) bands.add(-1 - index, "Beatles");

d

Assume we are using quicksort to sort an array in ascending order. Into which array location does quicksort's strategy place a pivot element after partitioning? a) lowest index in array still available b) highest index in array still available c) closer to its correct final location d) its final correct location

d

Complete the following code that is intended to provide a comparator interface that will be used to create an object that implements the Comparator interface so that object can compare Auto objects. ___________________________ { int compare(Auto a, Auto b); } a) public class Comparator<Auto> b) public class Comparator<Auto> implements Comparator c) public interface Auto<Comparator> d) public interface Comparator<Auto>

d

Consider an array with n elements. If we visit each element n times, how many total visits will there be? a) n b) 2n c) nn d) n^2

d

Consider the sort method shown below for selection sort: public static void sort (int[] a) { for (int i = 0; i < a.length - 1; i++) { int minPos = minimumPosition(i); swap(minPos, i); } } Suppose we modify the call to the swap method call to read swap(i, minPos). What would be the result? a) An exception would occur. b) The sort would produce incorrect results. c) The sort would work, but sort backwards. d) The sort would produce correct results.

d

If a call to the Arrays static method binarySearch returns a value of -10, what can be concluded? I the element is not in the array II the element is at index 10 III the element can be inserted at index 9 a) I b) II c) III d) I and III

d

If f(n) = O(g(n)) and g(n) = O(f(n)), what else must be true? I f(n) = Ω(g(n)) II g(n) = Ω(f(n)) III f(n) = θ(g(n)) a) I b) II c) I and II d) I, II, and III

d

In big-Oh notation, suppose an algorithm requires an order of n3 element visits. How does doubling the number of elements affect the number of visits? a) It doubles the number of visits. b) It quadruples the number of visits. c) It triples the number of visits. d) It number of visits goes up by a factor of eight.

d

In big-Oh notation, when we consider the order of the number of visits an algorithm makes, what do we ignore? I power of two terms II the coefficients of the terms III all lower order terms a) I b) II c) I and II d) II and III

d

In the textbook, we found that the number of element visits for merge sort totaled n + 5n log2 n. Which of the following is the appropriate big-Oh notation for merge sort? a) 5n log2 n b) n + log2 n c) n + 5n d) n log2 n

d

In the textbook, we found that the number of element visits for merge sort totaled n + 5nlog2n. Let's consider sorting 1024 elements. How many visits are needed? a) 1.024 b) 6,144 c) 51,200 d) 52,224

d

Selection sort has O(n^2) complexity. If a computer can sort 1,000 elements in 4 seconds, approximately how many seconds will it take the computer to sort 1,000 times that many, or 1,000,000 elements? a) 16 b) 1,000 c) 1,000,000 d) 4,000,000

d

Suppose objects a and b are from a user-defined class that implements the Comparable interface. What must be true about the return value of a.compareTo(b) for the compareTo method that this class implements? a) It must return 1 if a comes before b, 0 if they are the same, and -1 if a comes after b. b) It must return -1 if a comes before b, 0 if they are the same, and 1 if a comes after b. c) It must return a positive value if a comes before b, 0 if they are the same, and a negative value if a comes after b. d) It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b.

d

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(); } a) (int j = n; j > 0; j = j / 2) b) (int j = n; j > 0; j--) c) (int j = 1; j < k; j++) d) (int j = 1; j <= 10; j++)

d

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? a) Its running time will be O(n). b) Its running time will be O(n^2). c) Its running time will be O(log (n)). d) Its running time will be O(1).

d

When does quicksort's worst-case run-time behavior occur? I when the data is randomly initialized in the array II when the data is in ascending order III when the data is in descending order a) I b) II c) III d) II and III

d

When your class implements a comparator object, you must implement the compare method. What must be true about the return value from this method when comparing two objects, a and b with a call to a.compare(b)? a) It must return 1 if a comes before b, 0 if they are the same, and -1 if a comes after b. b) It must return -1 if a comes before b, 0 if they are the same, and 1 if a comes after b. c) It must return a positive value if a comes before b, 0 if they are the same, and a negative value if a comes after b. d) It must return a negative value if a comes before b, 0 if they are the same, and a positive value if a comes after b.

d

Which notation, big-Oh, theta, or omega describes the growth rate of a function? I big-Oh II theta III omega a) I b) II and III c) I and II d) I, II, and III

d

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 Comparable interface a) I b) II c) I and II d) I, II and III

d

Which of the sorts in the textbook are based on the strategy of divide and conquer? I quicksort II mergesort III insertion sort a) I b) II c) III d) I and II

d


Ensembles d'études connexes

Chapter 11 - Final Exam - MIE 330

View Set

ASCP Practice Questions Microbiology

View Set

Postoperative Nursing Management Chapter 19 PrepU N403

View Set

Practice - Pedagogy & Professional Responsibilities PPR

View Set

Панели "Ключ"-2 2 урок

View Set