Chapter 14: Sorting (Caces' class)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

a. (int j = 0; j < k; j++)

Selection sort has O(n2) 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. 4,000,000 b. 1,000,000 c. 16 d. 1,000

a. 4,000,000

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. II and III b. II c. I d. III

a. II and III

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 number of visits goes up by a factor of eight. b. It doubles the number of visits. c. It triples the number of visits. d. It quadruples the number of visits.

a. It number of visits goes up by a factor of eight.

Can you search the following array using binary search? int[] A = {6, 5, 4, 2, 0, 1, -1, -17}; a. No. Binary search can be applied to a sorted array only. b. Yes. Binary search can be applied to any array. 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.

a. No. Binary search can be applied to a sorted array only.

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 developer must supply a comparator object belonging to a class that implements the Comparator<XYZ> interface. b. XYZ objects must be randomly distributed. c. The XYZ class must implement the Sortable interface. d. XYZ objects must be ordered.

a. The developer must supply a comparator object belonging to a class that implements the Comparator<XYZ> interface.

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

a. int compare(Auto a, Auto b);

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. n / 2

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

a. θ(n^1/2)

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

b. 12

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

b. 52,224

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

b. 9th iteration

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 and III b. III c. II d. I

b. III

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

b. The smallest element not yet placed in prior iterations.

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

b. a[mid].compareTo(item)

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

b. f(n) = O(g(n))

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. quicksort b. insertion sort c. selection sort d. merge sort

b. insertion sort

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++) b. (int j = 0; j < array.length; j = j + 2) c. (int j = 1; j < array.length; j = j * 2) d. (int j = 0; j < array.length / 2; j++)

c. (int j = 1; j < array.length; j = j * 2)

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

c. 7

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

c. quicksort

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. I and III c. III d. II

d. II

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

d. O(n2)

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^6 c. n^2 d. n^3

d. n^3

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. I and II

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 25 times longer b. Approximately 20 times longer c. Approximately 100 times longer d. Approximately 5 times longer

a. Approximately 25 times longer

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 and III b. I c. II d. III

a. I and III

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

a. I and III

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, II and III b. II c. I d. I and II

a. I, II and III

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]; } } 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 log (n)). c. Its running time will be O(n2). d. Its running time will be O(log (n)).

a. Its running time will be O(n).

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 both in the same half of the array. c. They are in different halves of the array. d. They are adjacent.

a. They are randomly located.

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

a. faster than

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. privatestatic 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

a. first[iFirst] > second[iSecond]

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 interface Comparator<Auto> b. public class Comparator<Auto> c. public interface Auto<Comparator> d. public class Comparator<Auto> implements Comparator

a. public interface Comparator<Auto>

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. 2 d. 3

b. 4

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 1,200,000 records. b. Approximately 1,000,000 records. c. Approximately 75,000 records. d. Approximately 50,000 records.

b. Approximately 1,000,000 records.

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

b. O(n log(n))

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

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

b. One element visit before a recursive call on one half of the array.

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

b. n^2

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. -6 c. -5 d. 6

c. -5

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

c. I, II, and III

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(log (n)) c. O(n) d. O(n log (n))

c. O(n)

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

c. O(n)

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 = ____________________________; a. System.currentTimeMillis() b. runningTime + start - System.currentTimeMillis() c. System.currentTimeMillis() - start d. System.currentTimeMillis() + start

c. System.currentTimeMillis() - start

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. The sort would produce incorrect results. b. The sort would work, but sort backwards. c. The sort would produce correct results. d. An exception would occur.

c. The sort would produce correct results.

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. Make a file with many sets of values and loop through them, sorting each one. b. Run the program many times, entering different values for the array elements. 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. Use the Random class to generate array elements, sorting each set in a loop.

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; }

c. if(a[i] > a[maxPos]) { maxPos = i; }

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

c. θ(n3)

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

d. Collections

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 a positive value if a comes before b, 0 if they are the same, and a negative value 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 -1 if a comes before b, 0 if they are the same, and 1 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. 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.

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

d. Its running time will be O(log (n)).

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

d. O(n)

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

d. One element must be correctly placed.

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

d. When determining the running time, constants are not taken into consideration.

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) { __________________________ } a. bands.add(-1 * index, "Beatles"); b. bands.add(index + 1, "Beatles"); c. bands.add(-1 * index + 1, "Beatles"); d. bands.add(-1 - index, "Beatles");

d. bands.add(-1 - index, "Beatles");


Set pelajaran terkait

Exam 6 Study of Fundamentals in Chapter 10: Leadership, Managing and Delegating - Prep U

View Set

Psych Unit 7.1 Review- Motivation and Emotion

View Set

UNIT #11: Types of investment risks

View Set

section 12 unit 1: The Loan Process and Qualifying the Borrower

View Set