Algorithms and Recursion

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

How many times does the following loop iterate? // x has been initialized with a positive int value int count = 0; while (count < x) { count++; } A. x times B. x + 1 times C. x - 1 times D. Infinite number of times E. The condition is never true so this loop never executes.

A. x times

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? A. 3 B. 2 C. 10 D. 5

A. 3 First it compares 12, then 22 and then finds 42.

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? A. n - 1 B. n C. n (n - 1) D. n + 1

A. n - 1 Insertion sort is always (n - 1) while linear/sequential search is always (n + 1)

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

B. 10

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 3? int count = 0; while (count < 5) { System.out.println("CodeHS Rocks!"); count++; } // point 3 A. count < 5 is always true at point 3 B. count < 5 always false at point 3 C. count < 5 is sometimes true/sometimes false at point 2

B. count < 5 always false at point 3

How many times does the following loop iterate? // x has been initialized with a positive int value for (int count = 0; count <= x; count++) { System.out.print("*"); } A. x times B. x + 1 times C. x - 1 times D. Infinite number of times E. The condition is never true so this loop never executes.

B. x + 1 times

Which of the following sorting algorithms use recursion? A. Selection Sort B. Insertion Sort C. Merge Sort

C. MergeSort

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 2? int count = 0; while (count < 5) { System.out.println("CodeHS Rocks!"); count++; // point 2 } A. count < 5 is always true at point 2 B. count < 5 always false at point 2 C. count < 5 is sometimes true/sometimes false at point 2

C. count < 5 is sometimes true/sometimes false at point 2

How many times does the following loop execute? // x has been initialized with a positive int value greater than 5 int count = 5; while (count < x) { count++; } A. x times B. x - 1 times C. x - 5 times D. Infinite number of times E. The condition is never true so this loop never executes.

C. x - 5 times

Given the method below, how many times will the method doSort be called if an array ages contains the value [23, 12, 9, 46, 34] and sort(ages) is called? 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++; } } A. 12 B. 6 C. 9 D. 5 E. 15

C. 9

The following array is to be sorted biggest to smallest using insertion sort. [10, 40, 50, 30, 20, 60] What will the array look like after the third pass of the for loop? A. [60, 50, 40, 30, 20, 10] B. [50, 40, 10, 30, 20, 60] C. [50, 40, 30, 10, 20, 60] D. [60, 50, 40, 30, 10, 20] E. [10, 30, 40, 50, 20, 60]

C. [50, 40, 30, 10, 20, 60] Only dive the array after the third pass.

What is the precondition for binary search to work on an array? A. The array must be sorted. B. The array must contain only integers. C. The array must be of even size. D. The array must be of odd size. E. The element being searched for must be in the array.

A. The array must be sorted

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

A. The elements must be sorted.

Consider the following code snippet. Is count < 5 always true, always false, or sometimes true/sometimes false at point 1? int count = 0; while (count < 5) { // point 1 System.out.println("CodeHS Rocks!"); count++; } A. count < 5 is always true at point 1 B. count < 5 always false at point 1 C. count < 5 is sometimes true/sometimes false at point 1

A. count < 5 is always true at point 1

What approach does MergeSort use? A. divide and conquer B. iterative C. search and swap D. search and insert

A. divide and conquer

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)) A. 5 B. 6 C. 4 D. 10 E. The recursion will go on forever because there is no base case.

B. 6 There will be 6 calls. mystery(5), mystery(4), mystery(3), mystery(2), mystery(1), and mystery(0). n == 0 is the base case.

Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements? A. 128 B. 256 C. 4 D. 8 E. 16

D. 8 (log base 2 of 256 is 8)

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!") }

B. The middle iteration has no "+ 1" or "- 1" after it.

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

B. They must be Comparable.

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? A. 3 B. 2 C. 12 D. 5

C. 12 Linear search always does a number of comparisons equal to the index + 1 of the item to be found.`

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? A. 9 B. 6 C. 14 D. 10

C. 14 14 can never be found because it is out of order. Binary search will check 9, then go greater to 10, then go greater to 21, then the search is over.

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

C. 6

What is the correct pseudocode for Insertion Sort? A. If there are at least two elements in the collection Partition the collection Insertion sort the left collection Insertion sort the right collection B. 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 C. For each element in the collection 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 D. While there are unsorted numbers Find the smallest unsorted number Insert the element at the end of the sorted elements

C. For each element in the collection 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

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++; } } A. Selection sort B. Insertion sort C. Mergesort D. Binary search E. Sequential search / Linear search

C. MergeSort

How many comparisons will the selection sort algorithm make on an array of 8 elements? A. 3 B. 8 C. 24 D. 28 E. 64

D. 28

Why is having efficient algorithms important? A. It reduces the cost of running a program. B. It can improve the speed that programs operate. C. It increases the speed of innovations. D. Both 1 and 2. E. Both 2 and 3.

D. Both 1 and 2.

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

D. It uses more memory. Mergesort needs temporary arrays the same size as the array we are sorting.

When will method myMethod cause a stack overflow (i.e., cause computer memory to be exhausted)? public static int myMethod (int y, int z) { if (y > z) return y * z; else return myMethod(y - 2, z); } A. The method will never cause a stack overflow B. For all values y and z C. Only when y = 2 D. Only when y ≤ z E. Only when y > z

D. Only when y ≤ z

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? A. key is the last element in the array B. key is in the middle of the array C. n is very large D. key is the first element in the array E. key does not exist in the array

D. key is the first element in the array 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.

Question: 12 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 A. I only B. II only C. II and IV D. II and III E. 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.

False Linear Search and Sequential Search are only more efficient if an array is unsorted.


Set pelajaran terkait

TestOut Client Pro 1.3.6 - 7.2.8

View Set

Explain the main goal of chemistry

View Set

Exam II. Chapters 7-12 in Cockerham

View Set

AP Human Geography - Unit 2 TEST STUDY GUIDE Part 1

View Set

Nutrition Chapter 5: Carbohydrates DR AL

View Set

Moneyball: The Art of Winning an Unfair Game

View Set