Hanoi puzzle
public class Main {
static int nDisks = 3;
public static void main(String[] args) {
hanoiTower(nDisks, 'A', 'B', 'C');
}
public static void hanoiTower(int topN, char src, char inter, char dest) {
if (topN == 1)
System.out.println("Disk 1 from " + src + " to " + dest);
else {
// src to inter
hanoiTower(topN - 1, src, dest, inter);
// move bottom
System.out.println("Disk " + topN + " from " + src + " to " + dest);
//inter to dest
hanoiTower(topN - 1, inter, src, dest);
}
}
}
Output:
Disk 1 from A to C
Disk 2 from A to B
Disk 1 from C to B
Disk 3 from A to C
Disk 1 from B to A
Disk 2 from B to C
Disk 1 from A to C
Home
Java Book
Runnable examples
Java Book
Runnable examples
Algorithm:
- Binary search
- Binary Search Insert
- Recursive Binary Search Implementation in Java
- Linear Searching double Arrays
- Bubble sort
- Heap sort
- Merge sort
- Fast Merge Sort
- Fast Quick Sort
- Simple version of quick sort
- Quicksort for sorting arrays
- Quick Sort Implementation with median-of-three partitioning and cutoff for small arrays
- Quick sort with median-of-three partitioning
- Insert sort
- Insert Sort for objects
- Selection sort
- Shell sort
- Fibonacci
- Hanoi puzzle
- Table of fahrenheit and celsius temperatures
- Growable integer array
- Linked List class
- Binary Tree
- Tree with generic user object