Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C Program for Tower of Hanoi, Assignments of Data Structures and Algorithms

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1) Only one disk can be moved at a time. 2) Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 3) No disk may be placed on top of a smaller disk.

Typology: Assignments

2022/2023

Uploaded on 10/15/2023

maroof-ahmad-1
maroof-ahmad-1 🇮🇳

1 document

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Towers of Hanoi
Move n (4) disks from pole A to pole C
such that a disk is never put on a smaller disk
A
AB
BC
C
A
AB
BC
C
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download C Program for Tower of Hanoi and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Towers of Hanoi

Move n (4) disks from pole A to pole C

such that a disk is never put on a smaller disk

AA^ BB CC AA BB (^) CC

AA BB CC  (^) Move n (4) disks from A to C  (^) Move n-1 (3) disks from A to B  (^) Move 1 disk from A to C  (^) Move n-1 (3) disks from B to C

Figure 2.19c and dFigure 2.19c and d

c) move one disk from A to B ; d) move n - 1 disks from C to B

Hanoi towers

public static void solveTowers(int count, char source, char destination, char spare) { if (count == 1) { System.out.println("Move top disk from pole " + source + " to pole " + destination); } else { solveTowers(count-1, source, spare, destination); // X solveTowers(1, source, destination, spare); // Y solveTowers(count-1, spare, destination, source); // Z } // end if } // end solveTowers

AA BB CC AA BB CC AA^ BB^ CC

Figure 2.21a Figure 2.21a

Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA BB CC

Figure 2.21bFigure 2.21b

Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA^ BB^ CC AA^ BB^ CC AA BB CC AA^ BB^ CC

Figure 2.21dFigure 2.21d

Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA^ BB^ CC

Figure 2.21eFigure 2.21e

Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)

Recursive Searching

Linear Search

Binary Search

Find an element in an array, return its

position (index) if found, or -1 if not found.

Linear Search Algorithm (Java)

public int linSearch(int[] arr, int target) { for (int i=0; i<arr.size; i++) { if (target == arr[i]) { return i; } } //for return -1; //target not found }

Recursive Linear Search

Algorithm

public int recLinSearch(int[] arr,int low,int x) { if (low >= arr.length) { // reach the end return -1; } else if (x == arr[low]){ return low; } else return recLinSearch(arr, low + 1, x); } }  (^) Base case  (^) Found the target or  (^) Reached the end of the array  (^) Recursive case  (^) Call linear search on array from the next item to the end

Binary Search Sketch

 (^) Linear search runs in O(n) (linear) time (it requires n comparisons in the worst case)  (^) If the array to be searched is sorted (from lowest to highest), we can do better:  (^) Check the midpoint of the array to see if it is the item we are searching for  (^) Presumably there is only a 1/n chance that it is! (assuming that the target is in the array)  (^) It the value of the item at the midpoint is less than the target then the target must be in the upper half of the array  (^) So perform binary search on that half  (^) and so on ….

Recursive Binary Search

Algorithm

public int binSearch( int[] arr, int lower, int upper, int x) { int mid = (lower + upper) / 2; if (lower > upper) {// empty interval return - 1; // base case } else if(arr[mid] == x){ return mid; // second base case } else if(arr[mid] < x){ return binSearch(arr, mid + 1, upper, x); } else { // arr[mid] > target return binSearch(arr, lower, mid - 1, x); } }

Analyzing Binary Search

 (^) Best case : 1 comparison  (^) Worst case : target is not in the array, or is the last item to be compared  (^) Each recursive call halves the input size  (^) Assume that n = 2 k^ (e.g. if n = 128, k = 7)  (^) After the first iteration there are n /2 candidates  (^) After the second iteration there are n /4 (or n /2^2 ) candidates  (^) After the third iteration there are n /8 (or n /2^3 ) candidates  (^) After the k- th iteration there is one candidate because n/2 k^ = 1  (^) Because n = 2 k , k = log 2 n  (^) Thus, at most k=log 2 n^ recursive calls are made in the worst case!