Chapter 6 CompSci

Ace your homework & exams now with Quizwiz!

What must be true before performing a binary search? The elements must be sorted. It can only contain binary values. The elements must be some sort of number (i.e. int, double, Integer) There are no necessary conditions.

A- The elements must be sorted

Given this array: 1 2 4 5 6 7 8 12 14 21 22 42 53 How many comparisons are required to find 42 using the Binary Search? 3 2 10 5

A. 3

For this array of numbers: [1, 2, 3, 4, 5] How many comparisons are made using Selection Sort? 5 10 1 6 11

B. 10

Consider the following recursive function: public int mystery(int n) { if(n == 0) { return 2; } else { return 2 * mystery(n - 1); } } How many times will the function mystery be called if we call mystery(5) (be sure to include the first call mystery(5)) 5 6 4 10 The recursion will go on forever because there is no base case.

B. 6

Given this array: 1 2 4 5 6 7 8 12 14 21 22 42 53 How many comparisons are required to find 42 using the Linear Search / Sequential Search? 3 2 12 5

C. 12 (n-1?)

What is the largest number of comparisons needed to perform a binary search on an array with 42 elements? 2 5 6 41 42

C. 6

Which sorting method is implemented below? public int[] array; public int[] tempArr; public int length; public void sort(int[] inputArr) { array = inputArr; length = inputArr.length; tempArr = new int[length]; doSort(0, length - 1); } private void doSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; doSort(lowerIndex, middle); doSort(middle + 1, higherIndex); doSomething(lowerIndex, middle, higherIndex); } } private void doSomething(int lowerIndex, int middle, int higherIndex) { for (int i = lowerIndex; i <= higherIndex; i++) { tempArr[i] = array[i]; } int i = lowerIndex; int j = middle + 1; int k = lowerIndex; while (i <= middle && j <= higherIndex) { if (tempArr[i] <= tempArr[j]) { array[k] = tempArr[i]; i++; } else { array[k] = tempArr[j]; j++; } k++; } while (i <= middle) { array[k] = tempArr[i]; k++; i++; } } Selection sort Insertion sort Mergesort Binary search Sequential search / Linear search

C. mergesort

What are the drawbacks of using mergesort? It has no drawbacks. It only works on arrays. There are special cases where it runs very slowly. It uses more memory.

D.

We are searching for an int key in a sorted int array that has n elements. Under what circumstances will Linear Search / Sequential Search be more efficient than Binary Search? key is the last element in the array key is in the middle of the array n is very large key is the first element in the array key does not exist in the array

D. Linear search checks the first element first, so it will immediately find the `key` if `key` is the first element in the array. Binary search will take longer to check the first value because it cuts the array in half each time and checks the middle element.

What is the correct pseudocode for Insertion Sort? If there are at least two elements in the collection Partition the collection Insertion sort the left collection Insertion sort the right collection If there is more than one element in the collection Break the collection into two halves Insertion sort the left half Insertion sort the right half Compare the two halves Merge the two subcollections into a sorted collection Start with the first element and sort it with itself While there is a next element Compare the next unsorted element with the sorted elements Insert the element into the correct position in the sorted elements While there are unsorted numbers Find the smallest unsorted number Insert the element at the end of the sorted elements

Start with the first element and sort it with itself While there is a next element Compare the next unsorted element with the sorted elements Insert the element into the correct position in the sorted elements

What approach does MergeSort use? divide and conquer iterative search and swap search and insert

A.

Which of the following sorting algorithms use recursion? Selection Sort Insertion Sort Merge Sort

C.

Suppose we have a sorted list with n elements. We decide to perform an insertion sort on this already sorted set. How many comparisons will the sort make? n - 1 n n (n - 1) n + 1

A. n-1

Which of the following functions is a proper implementation of binary search? A. public static int binarySearch(int[] array, int key) { int n = array.length; int first = 0; int last = n - 1; int middle = (first + last)/2; while( first <= last ) { if ( array[middle] < key ) { first = middle + 1; } else if ( array[middle] == key ) { return (middle + 1); } else { last = middle - 1; } middle = (first + last)/2; } return -1; } B. public static int binarySearch(int[] array, int key) { int n = array.length; int first = 0; int last = n - 1; int middle = (first + last)/2; while( first <= last ) { if ( array[middle] < key ) { first = middle + 1; } else if ( array[middle] == key ) { return middle; } else { last = middle - 1; } middle = (first + last)/2; } return -1; } C. public static int binarySearch(int[] array, int key) { n = array.length; first = 0; last = n - 1; middle = (first + last)/2; while( first <= last ) { if ( array[middle] < key ) { first = middle + 1; } else if ( array[middle] == key ) { System.out.println(key + " found at index " + middle); } else { last = middle - 1; } middle = (first + last)/2; } System.out.println("Key not found!") } D. public static int binarySearch(int[] array, int key) { int n = array.length; int first = 0; int last = n - 1; int middle = (first + last)/2; while( first <= last ) { if ( array[middle] < key ) { first = middle + 1; } else if ( array[middle] == key ) { int result = middle; } else { last = middle - 1; } middle = (first + last)/2; } }

B

What must be true in order to sort objects? They must be wrapper classes of primitives (Integer, Double, etc.) They must be Comparable. At least one instance variable must be a primitive. They must be Strings Objects are not sortable, only primitives are sortable.

B.

Given the method implemented above, how many times will the method doSort be called if an array ages contains the values [23, 12, 9, 46, 34] and sort(ages) is called? 12 6 9 5

C. 9

How are Selection Sort and Insertion Sort different? I - Selection Sort is always faster than Insertion Sort II - Insertion Sort is always faster than Selection Sort III - When Selection Sort places an element into the sorted part of the array, that element is in its final position, whereas Insertion Sort may move the element later if it finds a smaller element. Selection Sort builds up an absolutely sorted array as it goes while Insertion Sort builds up a relatively sorted array as it goes. IV - Insertion Sort is faster if the array is already sorted or close to sorted I only II only II and IV II and III III and IV

E. III and IV

True or false: If an array is already sorted, Linear Search / Sequential Search is more efficient than Binary Search. True False

FALSE

BMO the robot is programming a new game called "Open the box!" You give him a number and he tries to open a numbered box. He's using Binary Search to accomplish this. Unfortunately, the boxes are not sorted. They are in the following order: 1 3 6 9 14 10 21 Which box can NEVER be found using binary search? 9 6 14 10

c. 14


Related study sets

RT Micro Ch. 21 Parasite Infections

View Set

Management Info Systems Chapter 2

View Set

Quoting vs Paraphrasing vs Summarizing

View Set

chapter 16 -Leases, Options and Contracts for Deed

View Set