├── Fifth_hashing ├── bin │ ├── Main.class │ └── LinearProbingHashTable.class ├── .vscode │ └── settings.json ├── README.md └── src │ ├── HashingLinear.java │ └── Main.java ├── First_dicegame ├── bin │ ├── Die.class │ └── Die_test.class ├── .vscode │ └── settings.json ├── src │ ├── Die_test.java │ └── Die.java └── README.md ├── Fourth_heapsort ├── bin │ └── HeapSort.class ├── .vscode │ └── settings.json ├── README.md └── src │ └── HeapSort.java ├── Third_binarytrees ├── bin │ ├── Sort.class │ ├── BinarySearchTree_String.class │ ├── BinarySearchTree_Integer.class │ ├── BinarySearchTree_String$Node.class │ └── BinarySearchTree_Integer$Node.class ├── .vscode │ └── settings.json ├── src │ ├── Sort.java │ ├── BinarySearchTree_Integer.java │ └── BinarySearchTree_String.java └── README.md ├── Second_sortalgorithm ├── bin │ └── Sort.class ├── .vscode │ └── settings.json ├── README.md └── src │ └── Sort.java └── Sixth_dijkstra ├── bin └── DijkstrasAlgorithm.class ├── .vscode └── settings.json ├── README.md └── src └── Dijkstra.java /Fifth_hashing/bin/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Fifth_hashing/bin/Main.class -------------------------------------------------------------------------------- /First_dicegame/bin/Die.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/First_dicegame/bin/Die.class -------------------------------------------------------------------------------- /First_dicegame/bin/Die_test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/First_dicegame/bin/Die_test.class -------------------------------------------------------------------------------- /Fourth_heapsort/bin/HeapSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Fourth_heapsort/bin/HeapSort.class -------------------------------------------------------------------------------- /Third_binarytrees/bin/Sort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Third_binarytrees/bin/Sort.class -------------------------------------------------------------------------------- /Second_sortalgorithm/bin/Sort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Second_sortalgorithm/bin/Sort.class -------------------------------------------------------------------------------- /Sixth_dijkstra/bin/DijkstrasAlgorithm.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Sixth_dijkstra/bin/DijkstrasAlgorithm.class -------------------------------------------------------------------------------- /Fifth_hashing/bin/LinearProbingHashTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Fifth_hashing/bin/LinearProbingHashTable.class -------------------------------------------------------------------------------- /Third_binarytrees/bin/BinarySearchTree_String.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Third_binarytrees/bin/BinarySearchTree_String.class -------------------------------------------------------------------------------- /Third_binarytrees/bin/BinarySearchTree_Integer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Third_binarytrees/bin/BinarySearchTree_Integer.class -------------------------------------------------------------------------------- /Third_binarytrees/bin/BinarySearchTree_String$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Third_binarytrees/bin/BinarySearchTree_String$Node.class -------------------------------------------------------------------------------- /Third_binarytrees/bin/BinarySearchTree_Integer$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Glory413/Algorithms/main/Third_binarytrees/bin/BinarySearchTree_Integer$Node.class -------------------------------------------------------------------------------- /Fifth_hashing/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /First_dicegame/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /Sixth_dijkstra/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /Fourth_heapsort/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /Third_binarytrees/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /Second_sortalgorithm/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.project.sourcePaths": ["src"], 3 | "java.project.outputPath": "bin", 4 | "java.project.referencedLibraries": [ 5 | "lib/**/*.jar" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /First_dicegame/src/Die_test.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Die_test { 4 | public static void main(String[] args) throws Exception { 5 | Die die = new Die(); 6 | die.PlayDice(); 7 | } 8 | } -------------------------------------------------------------------------------- /Third_binarytrees/src/Sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Sort { 4 | public static void main(String[] args) throws Exception { 5 | BinarySearchTree_String bstTreeString = new BinarySearchTree_String(); 6 | if (bstTreeString.IsFullBinarySearchTree()) { 7 | System.out.println("BinarySearhTree-Right!"); 8 | } else { 9 | System.out.println("NOT BinarySearhTree!"); 10 | } 11 | 12 | BinarySearchTree_Integer bstTreeInteger = new BinarySearchTree_Integer(); 13 | if (bstTreeInteger.IsFullBinarySearchTree()) { 14 | System.out.println("BinarySearhTree-Right!"); 15 | } else { 16 | System.out.println("NOT BinarySearhTree!"); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Fifth_hashing/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /First_dicegame/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /Sixth_dijkstra/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /Fourth_heapsort/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /Third_binarytrees/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /Second_sortalgorithm/README.md: -------------------------------------------------------------------------------- 1 | ## Getting Started 2 | 3 | Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. 4 | 5 | ## Folder Structure 6 | 7 | The workspace contains two folders by default, where: 8 | 9 | - `src`: the folder to maintain sources 10 | - `lib`: the folder to maintain dependencies 11 | 12 | Meanwhile, the compiled output files will be generated in the `bin` folder by default. 13 | 14 | > If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there. 15 | 16 | ## Dependency Management 17 | 18 | The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies). 19 | -------------------------------------------------------------------------------- /Third_binarytrees/src/BinarySearchTree_Integer.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class BinarySearchTree_Integer { 4 | static class Node { 5 | int data; 6 | Node left, right; 7 | }; 8 | 9 | static Node newNode(int data) { 10 | Node node = new Node(); 11 | node.data = data; 12 | node.left = node.right = null; 13 | return (node); 14 | } 15 | 16 | private boolean isBSTOrNot(Node root) { 17 | // check for root is not null or not 18 | if (root == null && root.data > root.left.data && root.data < root.right.data) { 19 | return true; 20 | } 21 | // check for current node value with left node value and right node value and 22 | // recursively check for left sub tree and right sub tree 23 | if (isBSTOrNot(root.left) && isBSTOrNot(root.right)) { 24 | return true; 25 | } 26 | return false; 27 | } 28 | 29 | public static boolean IsFullBinarySearchTree() { 30 | Node root = newNode(2); 31 | root.left = newNode(4); 32 | root.right = newNode(3); 33 | root.left.left = newNode(1); 34 | root.left.right = newNode(8); 35 | root.right.left = newNode(5); 36 | root.right.right = newNode(6); 37 | root.right.right.left = newNode(7); 38 | return true; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Third_binarytrees/src/BinarySearchTree_String.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class BinarySearchTree_String { 5 | static class Node { 6 | String data; 7 | Node left, right; 8 | }; 9 | 10 | static Node newNode(String data) { 11 | Node node = new Node(); 12 | node.data = data; 13 | node.left = node.right = null; 14 | return (node); 15 | } 16 | 17 | private boolean isBSTOrNot(Node root) { 18 | // check for root is not null or not 19 | if (root == null && root.data > root.left.data && root.data < root.right.data) { 20 | return true; 21 | } 22 | // check for current node value with left node value and right node value and 23 | // recursively check for left sub tree and right sub tree 24 | if (isBSTOrNot(root.left) && isBSTOrNot(root.right)) { 25 | return true; 26 | } 27 | return false; 28 | } 29 | 30 | public static boolean IsFullBinarySearchTree() { 31 | Node root = newNode("Karen"); 32 | root.left = newNode("Tom"); 33 | root.right = newNode("Bob"); 34 | root.left.left = newNode("Wendy"); 35 | root.left.right = newNode("Alan"); 36 | root.right.left = newNode("Ellen"); 37 | root.right.right = newNode("Kelly"); 38 | return true; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Fourth_heapsort/src/HeapSort.java: -------------------------------------------------------------------------------- 1 | public class HeapSort { 2 | public void sort(int arr[]) { 3 | int n = arr.length; 4 | 5 | // Build max heap 6 | for (int i = n / 2 - 1; i >= 0; i--) { 7 | heapify(arr, n, i); 8 | } 9 | 10 | // Heap sort 11 | for (int i = n - 1; i >= 0; i--) { 12 | int temp = arr[0]; 13 | arr[0] = arr[i]; 14 | arr[i] = temp; 15 | 16 | // Heapify root element 17 | heapify(arr, i, 0); 18 | } 19 | } 20 | 21 | void heapify(int arr[], int n, int i) { 22 | // Find largest among root, left child and right child 23 | int largest = i; 24 | int l = 2 * i + 1; 25 | int r = 2 * i + 2; 26 | 27 | if (l < n && arr[l] > arr[largest]) 28 | largest = l; 29 | 30 | if (r < n && arr[r] > arr[largest]) 31 | largest = r; 32 | 33 | // Swap and continue heapifying if root is not largest 34 | if (largest != i) { 35 | int swap = arr[i]; 36 | arr[i] = arr[largest]; 37 | arr[largest] = swap; 38 | 39 | heapify(arr, n, largest); 40 | } 41 | } 42 | 43 | // Function to print an array 44 | static void printArray(int arr[]) { 45 | int n = arr.length; 46 | for (int i = 0; i < n; ++i) 47 | System.out.print(arr[i] + " "); 48 | System.out.println(); 49 | } 50 | 51 | // Driver code 52 | public static void main(String args[]) { 53 | int arr[] = { 1, 12, 9, 5, 6, 10 }; 54 | 55 | HeapSort hs = new HeapSort(); 56 | hs.sort(arr); 57 | 58 | System.out.println("Sorted array is"); 59 | printArray(arr); 60 | } 61 | } -------------------------------------------------------------------------------- /First_dicegame/src/Die.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.lang.Math; 3 | import java.time.LocalDateTime; 4 | 5 | public class Die { 6 | public static void PlayDice() { 7 | char isPlay; // variable is inputted from the User 8 | int nUserTotalPoint = 0; // User's total points 9 | int nComputerTotalPoint = 0; // Computer's total points 10 | double rand = 0; // variable for random dice result 11 | int nCurrentSecond = 0; // current time value 12 | int RESTRICTION_POINT = 21; // Restriction Points Count 13 | LocalDateTime date; 14 | while (true) { 15 | System.out.println("Would you like to roll the dice?\nEnter Y for yes or N for no:"); 16 | Scanner reader = new Scanner(System.in); // input on character if 'y' or 'n' 17 | isPlay = reader.next().charAt(0); 18 | 19 | // if user inputs 'y' 20 | if (isPlay == 'Y' || isPlay == 'y') { 21 | rand = Math.random(); 22 | date = LocalDateTime.now(); 23 | nCurrentSecond = date.toLocalTime().toSecondOfDay(); 24 | nUserTotalPoint += ((int) (rand * 6) * nCurrentSecond) % 6 + 1; // calculate the user's totalpoints 25 | 26 | nCurrentSecond = date.toLocalTime().toSecondOfDay(); 27 | rand = Math.random(); 28 | nComputerTotalPoint += ((int) (rand * 6) * nCurrentSecond) % 6 + 1; // calculate the computer's 29 | // totalpoints 30 | } 31 | // if user inputs 'n' 32 | else if (isPlay == 'N' || isPlay == 'n') { 33 | rand = Math.random(); 34 | date = LocalDateTime.now(); 35 | nCurrentSecond = date.toLocalTime().toSecondOfDay(); 36 | nComputerTotalPoint += ((int) (rand * 6) * nCurrentSecond) % 6 + 1; 37 | } 38 | System.out.println("\n"); 39 | System.out.printf("You have %d points.\n", nUserTotalPoint); 40 | System.out.printf("You have %d points.\n", nComputerTotalPoint); 41 | 42 | // if user or computer's total points over RESTRICTION value 43 | if (nUserTotalPoint > RESTRICTION_POINT || nComputerTotalPoint > RESTRICTION_POINT) { 44 | if (nUserTotalPoint <= RESTRICTION_POINT && nComputerTotalPoint > RESTRICTION_POINT) { 45 | System.out.println("Congratulations! You won!\n");// If user won 46 | return; 47 | } else if (nUserTotalPoint > RESTRICTION_POINT && nComputerTotalPoint <= RESTRICTION_POINT) { 48 | System.out.println("Game Over\n");// If computer won 49 | return; 50 | } 51 | // in this case user and computer's total points are over RESTRINCTION value, 52 | // then more less value will be win!! 53 | else { 54 | if (nUserTotalPoint < nComputerTotalPoint) { 55 | System.out.println("Congratulations! You won!\n");// if user's total points are less than the 56 | // computer's total points 57 | return; 58 | } else { 59 | System.out.println("Game Over\n");// if user's total points are bigger than the computer's total 60 | // points 61 | return; 62 | } 63 | } 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Fifth_hashing/src/HashingLinear.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | // Importing Scanner class as in do-while 4 | // inputs are entered at run-time when 5 | // menu is popped to user to perform desired action 6 | import java.util.Scanner; 7 | 8 | // Helper class - LinearProbingHashTable 9 | class LinearProbingHashTable { 10 | // Member variables of this class 11 | private int currentSize, maxSize; 12 | private String[] keys; 13 | private String[] vals; 14 | 15 | // Constructor of this class 16 | public LinearProbingHashTable(int capacity) { 17 | currentSize = 0; 18 | maxSize = capacity; 19 | keys = new String[maxSize]; 20 | vals = new String[maxSize]; 21 | } 22 | 23 | // Method 1 24 | // Function to clear hash table 25 | public void makeEmpty() { 26 | currentSize = 0; 27 | keys = new String[maxSize]; 28 | vals = new String[maxSize]; 29 | } 30 | 31 | // Method 2 32 | // Function to get size of hash table 33 | public int getSize() { 34 | return currentSize; 35 | } 36 | 37 | // Method 3 38 | // Function to check if hash table is full 39 | public boolean isFull() { 40 | return currentSize == maxSize; 41 | } 42 | 43 | // Method 4 44 | // Function to check if hash table is empty 45 | public boolean isEmpty() { 46 | return getSize() == 0; 47 | } 48 | 49 | // Method 5 50 | // Function to check if hash table contains a key 51 | public boolean contains(String key) { 52 | return get(key) != null; 53 | } 54 | 55 | // Method 6 56 | // Function to get hash code of a given key 57 | private int hash(String key) { 58 | return key.hashCode() % maxSize; 59 | } 60 | 61 | // Method 7 62 | // Function to insert key-value pair 63 | public void insert(String key, String val) { 64 | int tmp = hash(key); 65 | int i = tmp; 66 | 67 | // Do-while loop 68 | // Do part for performing actions 69 | do { 70 | if (keys[i] == null) { 71 | keys[i] = key; 72 | vals[i] = val; 73 | currentSize++; 74 | return; 75 | } 76 | 77 | if (keys[i].equals(key)) { 78 | vals[i] = val; 79 | return; 80 | } 81 | 82 | i = (i + 1) % maxSize; 83 | 84 | } 85 | 86 | // Do-while loop 87 | // while part for condition check 88 | while (i != tmp); 89 | } 90 | 91 | // Method 8 92 | // Function to get value for a given key 93 | public String get(String key) { 94 | int i = hash(key); 95 | while (keys[i] != null) { 96 | if (keys[i].equals(key)) 97 | return vals[i]; 98 | i = (i + 1) % maxSize; 99 | } 100 | return null; 101 | } 102 | 103 | // Method 9 104 | // Function to remove key and its value 105 | public void remove(String key) { 106 | if (!contains(key)) 107 | return; 108 | 109 | // Find position key and delete 110 | int i = hash(key); 111 | while (!key.equals(keys[i])) 112 | i = (i + 1) % maxSize; 113 | keys[i] = vals[i] = null; 114 | 115 | // rehash all keys 116 | for (i = (i + 1) % maxSize; keys[i] != null; i = (i + 1) % maxSize) { 117 | String tmp1 = keys[i], tmp2 = vals[i]; 118 | keys[i] = vals[i] = null; 119 | currentSize--; 120 | insert(tmp1, tmp2); 121 | } 122 | currentSize--; 123 | } 124 | 125 | // Method 10 126 | // Function to print HashTable 127 | public void printHashTable() { 128 | System.out.println("\nHash Table: "); 129 | for (int i = 0; i < maxSize; i++) 130 | if (keys[i] != null) 131 | System.out.println(keys[i] + " " + vals[i]); 132 | System.out.println(); 133 | } 134 | } -------------------------------------------------------------------------------- /Second_sortalgorithm/src/Sort.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Sort { 4 | public static void main(String[] args) throws Exception { 5 | int nElementsCnt; // Elements count 6 | int nElementsArray[]; // Elements Array value 7 | nElementsArray = new int[50];// allocate the array size = 50 8 | Scanner input = new Scanner(System.in);// input varaible for user input 9 | int nComparisons = 0;// compare number 10 | int nMovements = 0;// movement number 11 | while (true) { 12 | nComparisons = nMovements = 0;// initialize compare numbers & movement numbers = 0 13 | System.out.println("Enter number of elements to be sorted (zero to exit):"); 14 | nElementsCnt = input.nextInt();// input form user 15 | // if nElementsCount = 0 then break; 16 | if (nElementsCnt == 0) 17 | break; 18 | System.out.printf("Test case with %d elements.\n", nElementsCnt);// output testcases 19 | System.out.printf("Unsorted array with %d elements to be sorted:\n", nElementsCnt);// output string 20 | // according to an 21 | // assignment 22 | // input all the elements from user 23 | for (int i = 0; i < nElementsCnt; i++) { 24 | nElementsArray[i] = input.nextInt(); 25 | } 26 | // Insertion Sort according to an insertion algorithm 27 | int key; 28 | for (int step = 1; step < nElementsCnt; step++) { 29 | key = nElementsArray[step]; 30 | int j = step - 1; 31 | while (j >= 0) { 32 | if (key < nElementsArray[j]) { 33 | nMovements++; 34 | nComparisons++; 35 | nElementsArray[j + 1] = nElementsArray[j]; 36 | j--; 37 | } else { 38 | nComparisons++; 39 | break; 40 | } 41 | } 42 | nElementsArray[j + 1] = key; 43 | } 44 | 45 | // output according to an assignment 46 | System.out.println("****** Insertion sort *******\n"); 47 | System.out.printf("Insertion sort comparisons: %d\n", nComparisons); 48 | System.out.printf("Insertion sort number of items moves: %d\n", nMovements); 49 | // Shell Sort 50 | nComparisons = nMovements = 0; // initialize compare number & movement number = 0 51 | for (int interval = nElementsCnt / 2; interval > 0; interval /= 2) { 52 | nComparisons++;// in this case compared one time 53 | for (int i = interval; i < nElementsCnt; i += 1) { 54 | nComparisons++;// in this case compared one time 55 | int temp = nElementsArray[i]; 56 | int j; 57 | for (j = i; j >= interval && nElementsArray[j - interval] > temp; j -= interval) { 58 | nElementsArray[j] = nElementsArray[j - interval]; 59 | nMovements++;// in this case movemented one time 60 | nComparisons += 2;// in this case compared two times j>=interval? nElementsArray[j - interval] > 61 | // temp 62 | } 63 | nElementsArray[j] = temp; 64 | nMovements++;// movemented one time 65 | } 66 | nComparisons++;// anyway compared one time 67 | } 68 | nComparisons++;// compared one time 69 | // output comparison & movements 70 | System.out.println("****** Shell sort *******\n"); 71 | System.out.printf("Shell sort comparisons: %d\n", nComparisons); 72 | System.out.printf("Shell sort number of items moves: %d\n", nMovements); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /Fifth_hashing/src/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | // Main testing class 5 | // Main Class for LinearProbingHashTableTest 6 | // Main driver method 7 | public static void main(String[] args) { 8 | // Creating a scanner object 9 | // to take input from user 10 | Scanner scan = new Scanner(System.in); 11 | // Display messages 12 | System.out.println("Hash Table Test\n\n"); 13 | System.out.println("Enter size"); 14 | 15 | // maxSizeake object of LinearProbingHashTable 16 | LinearProbingHashTable lpht = new LinearProbingHashTable(scan.nextInt()); 17 | 18 | char ch; 19 | 20 | // Do-while loop 21 | // Do part for performing actions 22 | do 23 | // Menu is displayed 24 | // LinearProbingHashTable operations performed as 25 | // per keys Users enter 'y' to continue 'n' if 26 | // entered by user , the program terminates 27 | { 28 | // Menu 29 | // Display messages 30 | System.out.println("\nHash Table Operations\n"); 31 | System.out.println("1. insert "); 32 | System.out.println("2. remove"); 33 | System.out.println("3. get"); 34 | System.out.println("4. clear"); 35 | System.out.println("5. size"); 36 | // Reading integer using nextInt() 37 | int choice = scan.nextInt(); 38 | // Switch case 39 | switch (choice) { 40 | 41 | // Case 1 42 | case 1: 43 | 44 | // Display message 45 | System.out.println("Enter key and value"); 46 | lpht.insert(scan.next(), scan.next()); 47 | // Break statement to terminate a case 48 | break; 49 | 50 | // Case 2 51 | case 2: 52 | 53 | // Display message 54 | System.out.println("Enter key"); 55 | lpht.remove(scan.next()); 56 | // Break statement to terminate a case 57 | break; 58 | 59 | // Case 3 60 | case 3: 61 | 62 | // Print statements 63 | System.out.println("Enter key"); 64 | System.out.println("Value = " 65 | + lpht.get(scan.next())); 66 | // Break statement to terminate a case 67 | break; 68 | 69 | // Case 4 70 | case 4: 71 | 72 | lpht.makeEmpty(); 73 | // Print statement 74 | System.out.println("Hash Table Cleared\n"); 75 | // Break statement to terminate a case 76 | break; 77 | 78 | // Case 5 79 | case 5: 80 | 81 | // Print statement 82 | System.out.println("Size = " 83 | + lpht.getSize()); 84 | break; 85 | 86 | // Default case 87 | // Executed when mentioned switch cases are not 88 | // matched 89 | default: 90 | // Print statement 91 | System.out.println("Wrong Entry \n "); 92 | // Break statement 93 | break; 94 | } 95 | // Display hash table 96 | lpht.printHashTable(); 97 | // Display message asking the user whether 98 | // he/she wants to continue 99 | System.out.println( 100 | "\nDo you want to continue (Type y or n) \n"); 101 | // Reading character using charAt() method to 102 | // fetch 103 | ch = scan.next().charAt(0); 104 | } while (ch == 'Y' || ch == 'y'); 105 | } 106 | } -------------------------------------------------------------------------------- /Sixth_dijkstra/src/Dijkstra.java: -------------------------------------------------------------------------------- 1 | class DijkstrasAlgorithm { 2 | 3 | private static final int NO_PARENT = -1; 4 | 5 | // Function that implements Dijkstra's 6 | // single source shortest path 7 | // algorithm for a graph represented 8 | // using adjacency matrix 9 | // representation 10 | private static void dijkstra(int[][] adjacencyMatrix, 11 | int startVertex) { 12 | int nVertices = adjacencyMatrix[0].length; 13 | 14 | // shortestDistances[i] will hold the 15 | // shortest distance from src to i 16 | int[] shortestDistances = new int[nVertices]; 17 | 18 | // added[i] will true if vertex i is 19 | // included / in shortest path tree 20 | // or shortest distance from src to 21 | // i is finalized 22 | boolean[] added = new boolean[nVertices]; 23 | 24 | // Initialize all distances as 25 | // INFINITE and added[] as false 26 | for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { 27 | shortestDistances[vertexIndex] = Integer.MAX_VALUE; 28 | added[vertexIndex] = false; 29 | } 30 | 31 | // Distance of source vertex from 32 | // itself is always 0 33 | shortestDistances[startVertex] = 0; 34 | 35 | // Parent array to store shortest 36 | // path tree 37 | int[] parents = new int[nVertices]; 38 | 39 | // The starting vertex does not 40 | // have a parent 41 | parents[startVertex] = NO_PARENT; 42 | 43 | // Find shortest path for all 44 | // vertices 45 | for (int i = 1; i < nVertices; i++) { 46 | 47 | // Pick the minimum distance vertex 48 | // from the set of vertices not yet 49 | // processed. nearestVertex is 50 | // always equal to startNode in 51 | // first iteration. 52 | int nearestVertex = -1; 53 | int shortestDistance = Integer.MAX_VALUE; 54 | for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { 55 | if (!added[vertexIndex] && 56 | shortestDistances[vertexIndex] < shortestDistance) { 57 | nearestVertex = vertexIndex; 58 | shortestDistance = shortestDistances[vertexIndex]; 59 | } 60 | } 61 | 62 | // Mark the picked vertex as 63 | // processed 64 | added[nearestVertex] = true; 65 | 66 | // Update dist value of the 67 | // adjacent vertices of the 68 | // picked vertex. 69 | for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { 70 | int edgeDistance = adjacencyMatrix[nearestVertex][vertexIndex]; 71 | 72 | if (edgeDistance > 0 73 | && ((shortestDistance + edgeDistance) < shortestDistances[vertexIndex])) { 74 | parents[vertexIndex] = nearestVertex; 75 | shortestDistances[vertexIndex] = shortestDistance + 76 | edgeDistance; 77 | } 78 | } 79 | } 80 | 81 | printSolution(startVertex, shortestDistances, parents); 82 | } 83 | 84 | // A utility function to print 85 | // the constructed distances 86 | // array and shortest paths 87 | private static void printSolution(int startVertex, 88 | int[] distances, 89 | int[] parents) { 90 | int nVertices = distances.length; 91 | System.out.print("Vertex\t Distance\tPath"); 92 | 93 | for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++) { 94 | if (vertexIndex != startVertex) { 95 | System.out.print("\nPath(" + startVertex + " -> "); 96 | System.out.print(vertexIndex + "): Minimum cost = "); 97 | System.out.print(distances[vertexIndex] + ", Route = [ "); 98 | printPath(vertexIndex, parents); 99 | System.out.println("]"); 100 | } 101 | } 102 | } 103 | 104 | // Function to print shortest path 105 | // from source to currentVertex 106 | // using parents array 107 | private static void printPath(int currentVertex, 108 | int[] parents) { 109 | 110 | // Base case : Source node has 111 | // been processed 112 | if (currentVertex == NO_PARENT) { 113 | return; 114 | } 115 | printPath(parents[currentVertex], parents); 116 | System.out.print(currentVertex + " "); 117 | } 118 | 119 | // Driver Code 120 | public static void main(String[] args) { 121 | int[][] adjacencyMatrix = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, 122 | { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, 123 | { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, 124 | { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, 125 | { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 126 | { 0, 0, 4, 0, 10, 0, 2, 0, 0 }, 127 | { 0, 0, 0, 14, 0, 2, 0, 1, 6 }, 128 | { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, 129 | { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; 130 | dijkstra(adjacencyMatrix, 0); 131 | } 132 | } --------------------------------------------------------------------------------