From 60eb361e1ec086f601fc0b26d013fd39399b3c03 Mon Sep 17 00:00:00 2001 From: Sachin Liyanage <59449070+Sachin-Liyanage@users.noreply.github.com> Date: Fri, 8 Oct 2021 20:10:45 +0530 Subject: [PATCH 1/2] Program for Tower of Hanoi in Python Language --- tower_of_hanoi.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 tower_of_hanoi.py diff --git a/tower_of_hanoi.py b/tower_of_hanoi.py new file mode 100644 index 0000000..819c5a8 --- /dev/null +++ b/tower_of_hanoi.py @@ -0,0 +1,23 @@ +# Recursive Python function to solve tower of hanoi + +def TowerOfHanoi(n , from_rod, to_rod, aux_rod): + if n == 1: + print("Move disk 1 from rod",from_rod,"to rod",to_rod) + return + TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) + print("Move disk",n,"from rod",from_rod,"to rod",to_rod) + TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) + +# Driver code +n = 4 +TowerOfHanoi(n, 'A', 'C', 'B') +# A, C, B are the name of rods + +""" +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: + +- Only one disk can be moved at a time. +- 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. +- No disk may be placed on top of a smaller disk. +""" \ No newline at end of file From 5cd27eb0d16ce323d8d579a61ab9c27dbeb4c8d7 Mon Sep 17 00:00:00 2001 From: Sachin Liyanage <59449070+Sachin-Liyanage@users.noreply.github.com> Date: Sun, 10 Oct 2021 23:35:09 +0530 Subject: [PATCH 2/2] Testing the Quick Sort algorithm using Java Unit Testing --- quick_sort_test.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 quick_sort_test.java diff --git a/quick_sort_test.java b/quick_sort_test.java new file mode 100644 index 0000000..ee84219 --- /dev/null +++ b/quick_sort_test.java @@ -0,0 +1,42 @@ +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Random; + +import org.junit.Before; +import org.junit.Test; + +public class QuicksortTest { + + private int[] numbers; + private final static int SIZE = 100000000; + private final static int MAX = 1000000; + + @Before + public void setUp() throws Exception { + numbers = new int[SIZE]; + Random generator = new Random(MAX); + for (int i = 0; i < numbers.length; i++) { + numbers[i] = generator.nextInt(MAX); + } + } + + @Test + public void testSort() { + long startTime = System.currentTimeMillis(); + + Quicksort sorter = new Quicksort(); + sorter.sort(numbers); + + for (int i = 0; i < numbers.length - 1; i++) { + if (numbers[i] > numbers[i + 1]) { + fail("Should not happen"); + } + } + assertTrue(true); + long stopTime = System.currentTimeMillis(); + long elapsedTime = stopTime - startTime; + System.out.println(elapsedTime); + + } +} \ No newline at end of file