Week 4 - Binary Search - Parameter Mechanism
Week 4 - Binary Search - Parameter Mechanism
Week 4:
Binary search (Book, Section 18.2)
Parameter mechanism (Book, section 7.16)
Towers of Hanoi (again)
Search algorithms
2
Linear search
3
Linear search:
Example: your Shop-class has a list of articles (myArticles), every
article has a unique idnr.
Search for an article with idnr equal to a certain searchkey (For
instance: search for the article with searchkey equal to a barcode or
an rfid-tag you just scanned).
4
Binary search
5
Binary search(only possible if the list is sorted by idnr):
public Article getArticle(int searchkey)
{ int lowindex, highindex;
lowindex = 0; highindex = myArticles.count – 1;
int m; // for middle index
while (lowindex <= highindex)
{ // P
m = (lowindex + highindex) / 2;
3 possibilities:
- myArticles[m].idnr == searchkey: return myArticles[m];
- myArticles[m].idnr < searchkey: lowindex = m + 1;
- myArticles[m].idnr > searchkey: highindex = m – 1;
} // P and not( lowindex <= highindex )
return null;
}
proposition P :
( Ei : 0<=i<myArticles.Count : myArticles[i].idnr ==
searchkey )
< == >
( Ei : lowindex<=i<=highindex : myArticles[i].idnr == 6
Binary search versus linear search:
• Linear search is easy to program, binary search needs more
thinking.
• Linear search works always; binary search only works for sorted
lists.
• Binary search is much, much, much faster.
7
PCS4
Week 4: Parameter
mechanism
(Book, section 7.16)
A method call: how about the parameters
and their associated values?
What happens when you call this method with actual values,
for instance 10 and a+b (a and b are integers),
like
DoIt ( 10, a + b );
9
An example
private void DoIt(int a, int b)
private int a; {
private int b; int temp;
a = 7; temp = a + b;
b = 13; a = 2 * temp;
b = a - b;
}
10
A method call:
11
private void DoIt(int a, ref int b)
{
An example int temp;
private int a; temp = a + b;
private int b; a = 2 * temp;
a = 7; b = a - b;
b = 13; }
14
Demo: show it!
15
Some rules on ref and out params in a method
• ref parameter
– ref parameter must be declared and initialized
before calling the method
– in the method, the value of the ref parameter may be
changed
• out parameter
– out parameter must be declared before calling the
method
– out parameter must be assigned a value in the
method
16
Example: Int32.TryParse
Demo:
Let’s implement our version of TryParse
17
Example: towers of Hanoi (again)
18
Hanoi, a difficult problem (if you don't like recursion)
3 piles (towers), numbered 1, 2, 3.
In how many moves can you solve the problem? And how should you
do it?
19
Hanoi, a difficult problem (if you don't like recursion)
20
Hanoi, a difficult problem (if you don't like recursion)
// Precondition: nrOfDiscs > 0.
public void Hanoi( int startPile,
int endPile,
int helpingPile,
int nrOfDiscs )
{
if (nrOfDiscs == 1)
{ DoAMove(startPile, endPile);
}
else
{ Hanoi(startPile, helpingPile, endPile, nrOfDiscs – 1);
DoAMove(startPile, endPile);
Hanoi(helpingPile, endPile, startPile, nrOfDiscs – 1);
}
}
21
Hanoi example with 3 discs
p1 p2 p3
• Hanoi(p1,p2,p3,3) p1 p2 p3
– Hanoi(p1,p3,p2,2)
• Hanoi(p1,p2,p3,1) p1 p2 p3
– Move(p1,p2)
• Move(p1,p3)
• Hanoi(p2,p3,p1,1) p1 p2 p3
– Move(p2,p3)
– Move(p1,p2) p1 p2 p3
– Hanoi(p3,p2,p1,2)
• Hanoi(p3,p1,p2,1) p1 p2 p3
– Move(p3,p1)
• Move(p3,p2)
• Hanoi(p1,p2,p3,1) p1 p2 p3
– Move(p1,p2)
p1 p2 p3
22