0% found this document useful (0 votes)
35 views

Week 4 - Binary Search - Parameter Mechanism

The document discusses binary search and linear search algorithms. It explains that binary search is faster than linear search but only works for sorted lists, while linear search always works. It also provides examples of parameter passing by value and by reference in methods. Finally, it introduces the Towers of Hanoi problem and provides a recursive solution to move disks between piles in the minimum number of moves.

Uploaded by

Rusnac Cristi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Week 4 - Binary Search - Parameter Mechanism

The document discusses binary search and linear search algorithms. It explains that binary search is faster than linear search but only works for sorted lists, while linear search always works. It also provides examples of parameter passing by value and by reference in methods. Finally, it introduces the Towers of Hanoi problem and provides a recursive solution to move disks between piles in the minimum number of moves.

Uploaded by

Rusnac Cristi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

PCS4

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).

Using linear search (inspect your articles one by one,


until you found what you are looking for, or
until you reach the end of the list):

public Article getArticle(int searchkey)


{
foreach ( Article a in this.myArticles)
{ if (a.idnr == searchkey ) { return a; }
}
return null;
}

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.

For n elements in the list,


- in linear search the average number of times the repetition will be
performed is ½ n if it is in the list and n times if it is not in the list.
- the body of the while-statement in a binary search will be executed
around 2log(n) times.

7
PCS4

Week 4: Parameter
mechanism
(Book, section 7.16)
A method call: how about the parameters
and their associated values?

public void DoIt ( int k, int m)


{
// some code here
}

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;
}

What happens when you push the button:

private void button1_Click(object sender, EventArgs e)


{
DoIt(10, a + b);
}

10
A method call:

• call by value if you specify nothing. Actual values


are calculated and used for the "placeholders".

• call by reference if you specify by ref (or by out).


Actual values for the "placeholders" are references
to memory-addresses.

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; }

What are a and b after clicking the button?


private void button1_Click(object sender, EventArgs e)
{
DoIt(10, a + b); //ERROR; a + b is not a reference
}

What are a and b after clicking the button?


private void button1_Click(object sender, EventArgs e)
{
DoIt(a + b, a);
}
// ERROR; a is not a reference.
12
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; }

What are a and b after clicking the button?


private void button1_Click(object sender, EventArgs e)
{
DoIt(10, ref a + b); //ERROR; a + b is not a reference
}

What are a and b after clicking the button?


private void button1_Click(object sender, EventArgs e)
{
DoIt(a + b, ref a);
}
// a has value 47 and b has value 13.
13
private int a;
An example private int b;
a = 7;
private void DoIt(int a, ref int b) b = 13;
{
int temp;
temp = a + b;
a = 2 * temp;
b = a - b;
}
private void Cute(ref int x, int y)
{
What are a and b after clicking the button?
x = x+1;
private void button1_Click(...)
y = y - 1;
{
DoIt( y , ref x );
Cute(ref a , b);
x++;
}
}
// a has value 33 and b has value 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

bool TryParse(string s, out int i)

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.

At startup: all discs are on pile 1 from "big to small".

At finish: all discs should be on pile 3.

A move: move a disc from one pile to another pile,


but you are not allowed to put a bigger disc on a smaller disc.

In how many moves can you solve the problem? And how should you
do it?

A solution without recursion: terrible to program.


A solution by using recursion: pretty simple.

19
Hanoi, a difficult problem (if you don't like recursion)

// Give me instructions about how to move


// nrOfDiscs discs from the pile numbered
// startPile to the pile numbered endPile.
// You may use the pile numbered helpingPile.
// Precondition: nrOfDiscs > 0.
public void Hanoi( int startPile,
int endPile,
int helpingPile,
int nrOfDiscs )
{
. . . // todo
}

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

You might also like