Professional Documents
Culture Documents
10 Binarysearch
10 Binarysearch
• Cost of ArraySet
• Simple Searching
• Binary Search
COMP103 – 2006/T3 2
Cost of ArraySet
• Operations are:
n
• contains(item)
• add(item) ← always add at the end
• remove(item) ← don’t need to shift down – just move last
item down
COMP103 – 2006/T3 4
Duplicate Detection
Add ‘Dog’
Add ‘Pig’
7
8
Question:
• How can we speed up the
search?
COMP103 – 2006/T3 5
How else can we search?
• Phone Book
• Data is ordered, so it is
• Faster to find items, because…
• We know roughly how far to look
Add ‘Cat’
COMP103 – 2006/T3 6
An improvement, but
AAABBBCCCDDDDEEEEFFFGGGHHHIIIJJJJJJJJJKKKKK
KLLLLLMMMMMNNOOOPPQQRRRSSSTTTTTUUVVV
VVWXXXYZZZ
COMP103 – 2006/T3 7
A better strategy…
COMP103 – 2006/T3 8
Making ArraySet faster.
• Binary Search: Finding “Eel”
8 0+7/2 4+4/2 =4 =5
= 3 4+7/2
COMP103 – 2006/T3 9
Binary Search
public boolean contains(E item){
Comparable<E> value = (Comparable<E>) item;
int low = 0; // min possible index of item
int high = count-1; // max possible index of item
// item in [low .. high] (if
present)
while (low <= high){
int mid = (low + high) / 2;
if (value.equals(data[mid]) = 0) // item is present
return true;
if (value.compareTo(data[mid]) < 0) // item in [low .. mid-1]
high = mid - 1; // item in [low .. high]
else // item in [mid+1 .. high]
low = mid + 1; // item in [low .. high]
}
return false; // item in [low .. high] and low > high,
// therefore item not present
}
COMP103 – 2006/T3 10
An Alternative
Return the index of where the item ought to be, whether present or not.
public int findIndex(Object item){
Comparable<E> value = (Comparable<E>) item;
int low = 0;
int high = count; // index in [low .. high]
while (low < high){
int mid = (low + high) / 2;
if (value.compareTo(data[mid]) > 0) // index in [mid+1 .. high]
low = mid + 1; // index in [low .. high] low <=
high
else // index in [low .. mid]
high = mid; // index in [low .. high],
low<=high
}
return low; // index in [low .. high] and low = high
// therefore index = low
}
COMP103 – 2006/T3 11
Exercise
• Calculate all the mid and return values from the
following searches using findIndex(…):
9 Find ‘Cat’
7 Find ‘Bug’
8 Find ‘Orc’
COMP103 – 2006/T3 12
Another Exercise
• Write a recursive binary search
below!
public int findIndex(E item, int low, int high){
Comparable<E> value = (Comparable<E>)item;
if(low >= high)
return low;
int mid = (low + high) / 2;
if (value.compareTo(data[mid]) > 0)
return recFind(itm, mid + 1, high);
else
return recFind(itm, low, mid);
COMP103 – 2006/T3 13
Binary Search: Cost
• What is the cost of searching if n items in set?
• key step = ? (look at the code)
0 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
k 1
COMP103 – 2006/T3 14
Log2(n ) :
The number of times you can divide a set of n things in half.
lg(1000) =10, lg(1,000,000) = 20, lg(1,000,000,000) =30
Every time you double n, you add one step to the cost!
Solve Solve
Solution
COMP103 – 2006/T3 15
ArraySet with Binary
Search
ArraySet: unordered
• All cost in the searching: O(n)
• contains: O(n )
• add: O(n )
• remove: O(n )
COMP103 – 2006/T3 16
Making SortedArraySet fast
• If you have to call add() and/or remove() many items,
then SortedArraySet is no better than ArraySet (we’ll see
how to do better later in the course)
• Both O(n )
• Either pay to search
• Or pay to keep it in order
• If you only have to construct the set once, and then many
calls to contains(),
then SortedArraySet is much better than ArraySet.
• SortedArraySet contains() is O(log(n )
COMP103 – 2006/T3 17
Alternative Constuctor
public SortedArraySet(Collection<E> c){
// Make space
count=c.size();
data = (E[]) new Object[count];
COMP103 – 2006/T3 18