CS 261 Winter 2009 Sorted Dynamic Array Bag and Set

advertisement
CS 261 Winter 2009
Sorted Dynamic Array
Bag and Set
The power of Ordered
Collections
• Why do you suppose that dictionaries or
phonebooks keep their elements in
order?
• Suppose I asked you to find the phone
number for Chris Smith?
• Suppose I asked you who was the
person with phone number 753-6692?
Gusss My Number
• We all know the heuristic of cutting a
collection in half from the game “guess
my number”.
• I’m thinking of a number between 1 and
100. What questions will you ask to find
my number?
Binary Search
• The formal name for this process is
Binary Search.
• Works by cutting the region containing
the value in half at each step.
• If I start out with n items, how many
times can I cut in half before I reach a
set of size one?
Log n search
• A log n search is much much fastar than
an O(n) search.
• Log of the largest integer value is still
less than 32!
Binary Search Algorithm
int binarySearch (double * data, int size, double testValue) {
int low = 0;
int high = size;
while (low < high) {
int mid = (low + high) / 2;
if (data[mid] < testValue))
low = mid + 1;
else high = mid;
}
return low;
}
What does this return
• If value is found in set, returns index
• If value is not in set, returns position
where it can be inserted without
violating ordering
• Note that this can be larger than a legal
index
Makes which Bag operation
faster?
• If we are using a dynamic array to store
values, Which of the following
operations is made faster by using a
binary search?
– Add(Element)
– Contains(Element)
– Remove(Element)
Your chance
• In a moment you will get your chance to
show how to implement a BAG using a
sorted dynamic array.
• But first, there is another reason for
keeping elements of a collection in
order.
• Fast merge, union and intersection
Fast Merge
Set operations are merge-like
• You can quickly merge two ordered
vectors into a NEW ordered vector.
• Intersection, union, different, subset are
similar to merge
Example, Intersection
Let I, j be index into two collections d, e
While I < size of d and j < size of e
If d[I] < e[j] advance I
Else if d[I] > e[j] advance j
Else they are equal, so add to
intersection and advance both
Example, Union (unique
elements)
Let I, j be index into collections d, e
While I < size of d and j < size of e
If d[I] < e[j] then add d[I] to union, I++
Else if d[I] > e[j] then add e[j] to union, j++
Else add d[I] to union, I++, j++
After loop, add rest of d and rest of e to union
Difference (D - E)
Let I, j be index into collections d, e
While I < size of d and j < size of e
If d[I] < e[j] then add d[I] to diff, I++
Else if d[I] > e[j] then j++
Else I++, j++
NOW it’s your turn
• Any questions?
• If not, then NOW you get your chance to
show that you understand how to make
a fast sortedArrayBag and a
sortedArraySet.
Download