Copyright © Cay S. Horstmann 2012 
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United
States License.
Estimated time: Three 40-minute sessions
javac Remove1.java java Remove1 1000 java Remove1 10000 java Remove1 100000 java Remove1 1000000
What do you notice?
time java Remove1 100000 time java Remove1 200000 time java Remove1 300000 time java Remove1 400000
Adjust the values as needed. For example, if you start with 70000, use 140000, 210000, 280000 for the other runs. What (user) times do you get? Make a table
| n1 | t1 |
| n2 | t2 |
| n3 | t3 |
| n4 | t4 |
where nk is the number for the kth run, and tk is the time.
| n1/n1 | t1/t1 |
| n2/n1 | t2/t1 |
| n3/n1 | t3/t1 |
| n4/n1 | t4/t1 |
What values do you get?
ArrayList to a LinkedList. How
do you expect the timing data to change? Why?$418K $439K $338K $410K $389K $405K $398K $388K $404K
what is the median?
Repeat until there are ≤2 elements. If there are two left, compute their average.
Look into the API documentation of the Collections class.
What library methods can you use for computing the maximum and minimum?
Look into the API documentation of the List class. What
library method can you use for removing a value (not an index)?
javac Median1.java java Median 10000
What value do you get?
Unfortunately, there is no library method for getting that position. Modify the algorithm in Section 7.6.4 of your textbook to get you the position of the largest value. What is the pseudocode?
Do this section if you have time. If not, go to D.
smallestIndex or largestIndex is one of the
last two positions in the array, then the smallest or largest element may
not be removed. Find a situation where this happens in the first
pass of the algorithm (with a short array). What is your array
contents?Collections class can you use to sort
the array?For simplicity, let's assume here that the array has odd length.
For example, the sequence
5 3 2 6 4 1 1 3 7
is partitioned, using the head element as the pivot, yielding the elements ≤ 5 and > 5, in some order.
3 3 2 1 4 1 | 6 5 7
In which part is the median? The first or the second? Why?
Note that we don't ever half to look at the other half again. That's the win over sorting, which would have to sort both halves.
select(k).
In a sorted array, what is the big-Oh efficiency of
select(k)?
select(k) without sorting. In step 1, you saw that you can
recursively call select(k) on the first part when the first
part has at least k elements. Time for a helper method:
select(k, 0, n - 1) = select(k, 0, p) if p = partition(0, n - 1) and k <= p
But it could equally well happen that the first part has < k elements. Then the kth element must be in the second half. But it won't be the kth element!
Construct an example where that happens—nine elements, where more than half are larger than the first. What is the initial sequence? What is the partitioned sequence?
Fill in:
select(k, 0, n - 1) = select(..., p + 1, n - 1) if p = partition(0, n - 1) and k > p
from need not be zero. 
for (int i = 1; i <= n; i++) lst.add(Math.random());
to
for (int i = 1; i <= n; i++) lst.add(i * 1.0);
What execution times do you get now?
Do this section if you have time.
Develop a recursive select2(k, from, to) that returns an
array of size 2 holding the kth and (k+1)st element. What is your
pseudocode?
select2. What is it?select2. What is your implementation?median with an even length
array. What is it?median to call select2 if the array
has even length. What is your code?Do this section if you have time.
int p = partition(from, to);
sort(from, p);
sort(p + 1, to);
What bad thing would happen if partition(from, to) returned
to? partition will return to, and that's ok.
Why?from < to and
partition(from, to) returns to? Let's look at
some simple cases. Walk through an array with elements
1 2 3 4 5 6
Walk through the call partition(0, 5). Make a walkthrough
table that shows how i and j change. What is your table?
to. Repeat for the array
6 5 4 3 2 1
to either. What if all the elements are
the same?
1 1 1 1 1 1
partition method:
private int partition(int from, int to)
{
int pivot = a[from];
int i = from - 1;
int j = to + 1;
while (i < j)
{
i++; while (a[i] < pivot) i++;
j--; while (a[j] > pivot) j--;
if (i < j) swap(i, j);
}
return j;
}
How do you know that it must be entered at least once?
(Remember the assumption that from < to).
j changes after the first traversal. The only
way that j can equal to is if the loop
while (a[j] > pivot) j--;
is never entered. Why?
u ? ? ? ? ? v
where u ≤ v. What is the contents of the array after the first iteration of the loop?