ESc101 : Fundamental of Computing
I Semester 2008-09
Lecture 34
• Fun : Tower of Hanoi - a classical recursive problem
• Binary Search
• Input/Output - Part 1
(using the existing classes and method of package java.io.*)
1
Tower of Hanoi
• There are three Towers : A,B,C
• Tower A has n discs arranged one above the other in the increasing order of
the radii from top to bottom.
• The towers B and C are empty.
• We can move one disc only in a single step.
• AIM : Describe the steps to transfer all discs from tower A to tower B.
Constraint : We can never place a bigger disc on smaller one.
2
Tower of Hanoi
• There are three Towers : A,B,C
• Tower A has n discs arranged one above the other in the increasing order of
the radii from top to bottom.
• The towers B and C are empty.
• We can move one disc only in a single step.
• AIM : Describe the steps to transfer all discs from tower A to tower B.
Constraint : We can never place a bigger disc on a smaller one.
3
Design a method Tower of Hanoi(n)
which prints the detailed instruction about the movement of discs in order to
transfer n discs from A to B .
4
Search
Problem : Given an array of large size, search whether an element is there ?
Easy solution : sequential search
what is we have to search 107 elements in an array of size 107
5
Search
Problem : Given an array of large size, search whether an element is there ?
Easy solution : sequential search
what if we have to search 107 elements in an array of size 107
6
Search
Problem : Given an array of large size, search whether an element is there ?
Easy solution : sequential search
what if we have to search 107 elements in an array of size 107
7
Binary Search : A powerful search algorithm
The code is given in file bin search.java.
8
Input Output from/to Console
Classes used :
• InputStreamReader
• BufferedReader
An interactive program for sorting numbers
see the file selection sort i.java
9
Input Output from/to Console
1. Reading characters one by one from console :
see the file Console Chareader.java
2. Reading Primitive types from console :
(first we read one complete input line as string and then convert that to the
primitive type)
see the file Interactively reading integers.java
10
Input Output from/to File - without buffers
To be discussed in next class
11