Exam 14: Sorting and Searching

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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)?

(Multiple Choice)
4.9/5
(42)

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

(Multiple Choice)
4.8/5
(42)

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?

(Multiple Choice)
4.8/5
(44)

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?

(Multiple Choice)
4.8/5
(32)

Given an ordered array with 15 elements, how many elements must be visited in the worst case of binary search?

(Multiple Choice)
4.7/5
(40)

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?

(Multiple Choice)
4.9/5
(39)

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

(Multiple Choice)
4.9/5
(34)

If the array is already sorted, what is the performance of insertion sort?

(Multiple Choice)
4.8/5
(37)

An algorithm that tests whether the first array element is equal to any of the other array elements would be an ____ algorithm.

(Multiple Choice)
4.8/5
(39)

In Big-Oh notation, selection sort is a(n) ____ algorithm.

(Multiple Choice)
4.7/5
(39)

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?

(Multiple Choice)
4.8/5
(41)

In the worst case, a linear search locates a value in an array of length n in ____ steps.

(Multiple Choice)
4.8/5
(36)

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

(Multiple Choice)
4.9/5
(37)

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?

(Multiple Choice)
4.8/5
(30)

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?

(Multiple Choice)
4.9/5
(36)

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?

(Multiple Choice)
4.9/5
(29)

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?

(Multiple Choice)
4.8/5
(38)

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?

(Multiple Choice)
4.8/5
(30)

An algorithm that cuts the work in half in each step is an ____ algorithm.

(Multiple Choice)
4.7/5
(34)

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?

(Multiple Choice)
4.9/5
(45)
Showing 61 - 80 of 99
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)