6.2.0 Unit 4 - Sorting & Recursive Algorithms Test Review
6.2.0 Unit 4 - Sorting & Recursive Algorithms Test Review
Definitions: binary search, base case, depth-first searching, infinite recursion, mergesort, insertion sort, recursion,
recursive call, selection sort, Big Oh notation
Interfaces: Comparable
1. What must be done to a list of items in order to use a binary search to find a specific item?
2. Use the recursive method below to answer the questions:
public void ct(int n) {
System.out.println(“Starting “ + n);
if (n > 0) {
ct(n/3);
System.out.println(“Middle “ + n);
}
}
3. A sorting algorithm is said to be “stable” if two items in the original array that have the same “key value” (the
value to be sorted on) maintain their relative position in the sorted version. For example, assume an array with
the following data:
Ann Jon Mel Tom Kim
20 19 18 19 22
When the array is sorted by age, a stable sort would guarantee that Jon would stay ahead of Tom in the sorted
array, as in:
Kim Ann Jon Tom Mel
22 20 19 19 18
and not:
Kim Ann Tom Jon Mel
22 20 19 19 18
Which of the sorts presented in this unit (selection, mergesort) is stable? For each which is not stable, give an
example of data to illustrate this.
Review Programs:
1. Create a Friends database application that maintains a file of Friends objects that contain names, telephone
numbers, and email addresses. The Friends application should load Friend records from a file and then allow the
user to add new friends, remove friends, display a list of friends by either first name or last name, and search for
a friend. The application should display a menu similar to:
1. List friends.
2. Add a friend.
3. Remove a friend.
4. Find a friend.
Enter 0 to quit
Enter your choice:
2. Modify the Searches class uses in the Binary Search lesson to include a ternarySearch() method. A ternary
serach, similar to a binary search divides an array into three pieces rather than two. A ternary search finds the
points that divide the array into three roughly equal pieces, and then uses these points to determine where the
goal should be searched for.