


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 26
This page cannot be seen from the preview
Don't miss anything!
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
c) move one disk from A to B ; d) move n - 1 disks from C to B
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
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA BB CC
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA^ BB^ CC AA^ BB^ CC AA BB CC AA^ BB^ CC
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’) AA^ BB^ CC
Box trace of solveTowers(3, ‘A’, ‘B’, ‘C’)
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 }
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
(^) 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 ….
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); } }
(^) 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!