APCSA CH 10

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

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?

14

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

Given the following recursive method public static int sumDigits(int number) { if (number == 0) { return 0; } int thisDigit = number%10; return thisDigit + sumDigits(number / 10); } Including the initial call, how many times will the sumDigits method be called if the initial call is sumDigits(4734)?

5

Consider the following recursive function: public static void printDigits(int number) { if (number == 0) { return; } System.out.println(number % 10); printDigits(number / 10); } What will be printed if printDigits(12345) is called?

5 4 3 2 1

Given the following code, how many calls (including the original call) to theprintX method would be made if we called with printX(20, 15); ?

6

How many times will the function mystery be called if we call mystery(5) (be sure to include the first call mystery(5))

6

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

6

The following recursive method is designed to adjust letter grades for students receiving a B, C, or D. public String adjustGrades(String grades) { if (grades.length() == 0) { return ""; } String thisGrade = grades.substring(0,1); if (thisGrade.equals("B")) { thisGrade = "A"; } else if (thisGrade.equals("C")) { thisGrade = "B"; } else if (thisGrade.equals("D")) { thisGrade = "C"; } return thisGrade + adjustGrades(grades.substring(1)); } Including the original call, how many times will the method be called using the following line of code: adjustGrades("AACDBA");

7

Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?

8

For a large dataset (roughly 100,000 items), which search is more efficient to find a value?

Binary searches are significantly more efficient.

Which best describes how a binary search works?

Binary searches start in the middle and eliminate half of the list in each iteration until the desired value is found.

Given the following recursive method public static int sumDigits(int number) { if (number == 0) { return 0; } int thisDigit = number%10; return thisDigit + sumDigits(number / 10); } Assuming number is a properly initialized and defined variable, which of the following loops will produce the same results?

Both I and II

Of the following problems, which one would a recursive function work best with?

Calculating factorial.Recall: 5 factorial = 5 * 4 * 3 * 2 * 1

What is the correct pseudocode for Merge Sort?

If there is more than one element in the collection Break the collection into two halves Merge sort the left half Merge sort the right half Compare the two halves Merge the two subcollections into a sorted collection

How are Insertion Sort and Merge Sort different?

Insertion Sort is faster if the array is already sorted or close to sorted

What is the definition of recursion?

Recursion is when a method calls itself.

What is the base case in a recursive statement?

The base case is the simplest problem to solve.

The following recursive method is intended to return the number of occurrences of a word from a phrase. 1: public static int countWord(String phrase, String word) 2: { 3: if (phrase.indexOf(word) < 0) 4: { 5: return 0; 6: } 7: return 1 + countWord(phrase.substring(phrase.indexOf(word)), word); 8: } Which of the following best describes why this method does not work as intended?

The first parameter of the recursive call should be phrase.substring(phrase.indexOf(word)+ 1)

T OR F: A binary search can be written either with a loop or with a recursive function.

True

why do we use recursion?

both of these options.

what approach does MergeSort use?

divide and conquer

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 first element in the array

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

mergesort

Given the following code: String number = "8735"; int sum = 0; for (int i = 0; i < number.length(); i++ ) { sum += Integer.parseInt(number.substring(i, i + 1)); } Which of the following recursive methods will produce the same result for mystery("8735")?

public static int mystery(String number) { if (number.length() == 0) { return 0; } int value = Integer.parseInt(number.substring(0, 1)); return value + mystery(number.substring(1)); }

What is the precondition for binary search to work on an array?

the array must be sorted.


Ensembles d'études connexes

Debt: Conceptual Issues, Long term/short term etc

View Set

D073 Best Practices in Management: Projects, Staffing, Scheduling, and Budgeting - D073

View Set

German tenses with 'essen', 'trinken' & 'kochen' (Present, Past, Future + "can" with 'ich')

View Set

Fundamentals Nursing Prep U Chapter 6 Values, Ethics, and Advocacy

View Set

Social Psychology Ch. 11 Prosocial Behavior

View Set

TestOut Security Pro, Lab Practice

View Set

Prepu: Chapter 6: Growth and Development of the School-age Child

View Set

Chapter 9 - Mechanisms of Memory formation

View Set