With the AP Comp Sci exam coming up in 2 days, I'd like to cover two algorithms that show up enough in the multiple choice to warrant some attention. These two algorithms are Binary Search and Bubblesort. Usually, these algorithms don't show up verbatim. Instead, you will see modifications to or applications modeled after these algorithms. For example, here is a Binary Search multiple choice question from the College Board course description for this year (2009-2010). Some elements of Binary Search that we can easily pick out are mid = (low+high)/2;, the modification of the range based on comparison of the elem at mid in comparison to the target, and our base case of low > high. In the truest sense of the AP, this question is a recursion question using the method name mystery(). The answer to this question is A, by the way.

I'd also like to look at how the AP sets up Bubblesort questions. To start, let's examine a traditional Bubblesort algorithm.

From the multiple choice questions I've seen, they take one of two approaches- they either take parts of Bubblesort or they modify the Bubblesort procedure so that it starts at different points for one of the two index variables (if a nested for loops approach is taken). For the first approach, the AP loves to use the inner for loop. It is usually set up as seen in the above implementation with the if statement inside; however, the code inside the if statement is varied for some other purpose. One example of the second approach, the modification of the sorting routine, shown below is an example (or something close to, as I'm recalling from memory) that was actually on the 2009 AP Computer Science Exam, that I saw as practice at my last AP Computer Science session. If you run it, then yes- it will sort an array of ints. However, is it a traditional Bubblesort we are used to seeing? No- or at least not for me. In both cases, the question provides an input and asks for the result.

A last warning- be very careful when you see an algorithm you recognize on the AP exam. The AP loves to throw those application questions at you, so make sure to work through the problem to get the correct answer. There is no reason that if you know what you are doing and you recognize the algorithm that you should lose points for assuming. It could make the difference between the score you get and the score you want.

Quote

10. Consider the following instance variable and method.

What is returned by the call

mystery(0, arr.length - 1, num) ?

(A) The number of elements in arr that are less than num

(B) The number of elements in arr that are less than or equal to num

© The number of elements in arr that are equal to num

(D) The number of elements in arr that are greater than num

(E) The index of the middle element in arr

private int[] arr; /** Precondition: arr contains no duplicates; * the elements in arr are in sorted order. * @param low 0 ≤ low ≤ arr.length * @param high low - 1 ≤ high < arr.length * @param num */ public int mystery(int low, int high, int num) { int mid = (low + high) / 2; if (low > high) { return low; } else if (arr[mid] < num) { return mystery(mid + 1, high, num); } else if (arr[mid] > num) { return mystery(low, mid - 1, num); } else // arr[mid] == num { return mid; } }

What is returned by the call

mystery(0, arr.length - 1, num) ?

(A) The number of elements in arr that are less than num

(B) The number of elements in arr that are less than or equal to num

© The number of elements in arr that are equal to num

(D) The number of elements in arr that are greater than num

(E) The index of the middle element in arr

I'd also like to look at how the AP sets up Bubblesort questions. To start, let's examine a traditional Bubblesort algorithm.

public static void bubbleSort(int[] x){ boolean swapped = false; do{ swapped = false; for(int i = 0; i < x.length-1; i++){ if(x[i] > x[i+1]){ swap(x, i, i+1); swapped = true; } } }while(swapped); }

From the multiple choice questions I've seen, they take one of two approaches- they either take parts of Bubblesort or they modify the Bubblesort procedure so that it starts at different points for one of the two index variables (if a nested for loops approach is taken). For the first approach, the AP loves to use the inner for loop. It is usually set up as seen in the above implementation with the if statement inside; however, the code inside the if statement is varied for some other purpose. One example of the second approach, the modification of the sorting routine, shown below is an example (or something close to, as I'm recalling from memory) that was actually on the 2009 AP Computer Science Exam, that I saw as practice at my last AP Computer Science session. If you run it, then yes- it will sort an array of ints. However, is it a traditional Bubblesort we are used to seeing? No- or at least not for me. In both cases, the question provides an input and asks for the result.

public static void mystery(int[] x){ for(int i = x.length-1; i > 0; i--){ for(int j = 0; j < i; j++){ if(x[j] > x[i]) swap(x, i, j); } }

A last warning- be very careful when you see an algorithm you recognize on the AP exam. The AP loves to throw those application questions at you, so make sure to work through the problem to get the correct answer. There is no reason that if you know what you are doing and you recognize the algorithm that you should lose points for assuming. It could make the difference between the score you get and the score you want.

### 2 Comments On This Entry

Page 1 of 1

#### Dogstopper

02 May 2010 - 07:31 AM
To be honest...I had to run the Binary Search one to see. OMG I felt stupid. I answered B last year. *facepalm*

Page 1 of 1

### ← August 2021 →

S | M | T | W | T | F | S |
---|---|---|---|---|---|---|

1 | 2 | 3 |
4
| 5 | 6 | 7 |

8 | 9 | 10 | 11 | 12 | 13 | 14 |

15 | 16 | 17 | 18 | 19 | 20 | 21 |

22 | 23 | 24 | 25 | 26 | 27 | 28 |

29 | 30 | 31 |

### My Blog Links

### Recent Entries

### Recent Comments

### Search My Blog

### 2 user(s) viewing

**2**Guests

**0**member(s)

**0**anonymous member(s)