diff --git a/.github/workflows/update_directory_md.yml b/.github/workflows/update_directory_md.yml new file mode 100644 index 000000000000..325fa04502af --- /dev/null +++ b/.github/workflows/update_directory_md.yml @@ -0,0 +1,67 @@ +# This GitHub Action updates the DIRECTORY.md file (if needed) when doing a git push +name: update_directory_md +on: [push] +jobs: + update_directory_md: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - uses: actions/setup-python@master + - name: update_directory_md + shell: python + run: | + import os + from typing import Iterator + + URL_BASE = "https://github.com/TheAlgorithms/Java/blob/master" + g_output = [] + + + def good_filepaths(top_dir: str = ".") -> Iterator[str]: + for dirpath, dirnames, filenames in os.walk(top_dir): + dirnames[:] = [d for d in dirnames if d[0] not in "._"] + for filename in filenames: + if os.path.splitext(filename)[1].lower() == ".java": + yield os.path.join(dirpath, filename).lstrip("./") + + + def md_prefix(i): + return f"{i * ' '}*" if i else "\n##" + + + def print_path(old_path: str, new_path: str) -> str: + global g_output + old_parts = old_path.split(os.sep) + for i, new_part in enumerate(new_path.split(os.sep)): + if i + 1 > len(old_parts) or old_parts[i] != new_part: + if new_part: + g_output.append(f"{md_prefix(i)} {new_part.replace('_', ' ')}") + return new_path + + + def build_directory_md(top_dir: str = ".") -> str: + global g_output + old_path = "" + for filepath in sorted(good_filepaths(), key=str.lower): + filepath, filename = os.path.split(filepath) + if filepath != old_path: + old_path = print_path(old_path, filepath) + indent = (filepath.count(os.sep) + 1) if filepath else 0 + url = "/".join((URL_BASE, filepath, filename)).replace(" ", "%20") + filename = os.path.splitext(filename.replace("_", " "))[0] + g_output.append(f"{md_prefix(indent)} [{filename}]({url})") + return "\n".join(g_output) + + + with open("DIRECTORY.md", "w") as out_file: + out_file.write(build_directory_md(".") + "\n") + + - name: Update DIRECTORY.md + run: | + cat DIRECTORY.md + git config --global user.name github-actions + git config --global user.email '${GITHUB_ACTOR}@users.noreply.github.com' + git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY + git add DIRECTORY.md + git commit -am "updating DIRECTORY.md" || true + git push --force origin HEAD:$GITHUB_REF || true diff --git a/Conversions/AnyBaseToAnyBase.java b/Conversions/AnyBaseToAnyBase.java index cfcf0fe9ad61..2cef99260e6b 100644 --- a/Conversions/AnyBaseToAnyBase.java +++ b/Conversions/AnyBaseToAnyBase.java @@ -52,6 +52,7 @@ public static void main(String[] args) { } } System.out.println(base2base(n, b1, b2)); + in.close(); } /** diff --git a/Conversions/AnytoAny.java b/Conversions/AnytoAny.java index 5fc744a9936c..1bc115404696 100644 --- a/Conversions/AnytoAny.java +++ b/Conversions/AnytoAny.java @@ -24,6 +24,7 @@ public static void main(String[] args) { dec /= db; } System.out.println(dn); + scn.close(); } } diff --git a/Conversions/DecimalToBinary.java b/Conversions/DecimalToBinary.java index 2c8d3218fdfe..cd84486ae812 100644 --- a/Conversions/DecimalToBinary.java +++ b/Conversions/DecimalToBinary.java @@ -5,7 +5,7 @@ /** * This class converts a Decimal number to a Binary number * - * @author Unknown + * */ class DecimalToBinary { @@ -53,6 +53,7 @@ public static void bitwiseConversion() { n >>= 1; } System.out.println("\tBinary number: " + b); + input.close(); } } diff --git a/Conversions/DecimalToHexaDecimal.java b/Conversions/DecimalToHexaDecimal.java index c9c4e434417c..3d0351dd0122 100644 --- a/Conversions/DecimalToHexaDecimal.java +++ b/Conversions/DecimalToHexaDecimal.java @@ -1,5 +1,6 @@ package Conversions; +//hex = [0 - 9] -> [A - F] class DecimalToHexaDecimal { private static final int sizeOfIntInHalfBytes = 8; private static final int numberOfBitsInAHalfByte = 4; diff --git a/Conversions/DecimalToOctal.java b/Conversions/DecimalToOctal.java index 017ab33321b5..98c9f1bb0c48 100644 --- a/Conversions/DecimalToOctal.java +++ b/Conversions/DecimalToOctal.java @@ -5,7 +5,7 @@ /** * This class converts Decimal numbers to Octal Numbers * - * @author Unknown + * */ public class DecimalToOctal { /** @@ -13,6 +13,8 @@ public class DecimalToOctal { * * @param args Command line Arguments */ + + //enter in a decimal value to get Octal output public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, k, d, s = 0, c = 0; diff --git a/Conversions/HexToOct.java b/Conversions/HexToOct.java index a46debfe9992..2807735f72fb 100644 --- a/Conversions/HexToOct.java +++ b/Conversions/HexToOct.java @@ -64,7 +64,6 @@ public static void main(String args[]) { // convert decimal to octal octalnum = decimal2octal(decnum); System.out.println("Number in octal: " + octalnum); - - + scan.close(); } } diff --git a/Conversions/HexaDecimalToBinary.java b/Conversions/HexaDecimalToBinary.java index 8a79e9b4f00c..091822ce0cb4 100644 --- a/Conversions/HexaDecimalToBinary.java +++ b/Conversions/HexaDecimalToBinary.java @@ -1,5 +1,7 @@ package Conversions; +//Hex [0-9],[A-F] -> Binary [0,1] + public class HexaDecimalToBinary { private final int LONG_BITS = 8; @@ -9,7 +11,7 @@ public void convert(String numHex) { int conHex = Integer.parseInt(numHex, 16); // Hex a Binary: String binary = Integer.toBinaryString(conHex); - // Presentation: + // Output: System.out.println(numHex + " = " + completeDigits(binary)); } diff --git a/Conversions/HexaDecimalToDecimal.java b/Conversions/HexaDecimalToDecimal.java index 533009b8ae16..14b0d94d76c2 100644 --- a/Conversions/HexaDecimalToDecimal.java +++ b/Conversions/HexaDecimalToDecimal.java @@ -34,7 +34,7 @@ public static void main(String args[]) { and it returns the decimal form in the variable dec_output. */ System.out.println("Number in Decimal: " + dec_output); - + scan.close(); } } diff --git a/Conversions/IntegerToRoman.java b/Conversions/IntegerToRoman.java index e979eb536336..8886ef4ad016 100644 --- a/Conversions/IntegerToRoman.java +++ b/Conversions/IntegerToRoman.java @@ -1,9 +1,29 @@ package Conversions; +/** + * Converting Integers into Roman Numerals + * + *('I', 1); + *('IV',4); + *('V', 5); + *('IV',9); + *('X', 10); + *('XL',40; + *('L', 50); + *('XC',90); + *('C', 100); + *('D', 500); + *('M', 1000); + * + */ + + public class IntegerToRoman { private static int[] allArabianRomanNumbers = new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; private static String[] allRomanNumbers = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + //Value must be > 0 + public static String integerToRoman(int num) { if (num <= 0) { return ""; diff --git a/Conversions/OctalToHexadecimal.java b/Conversions/OctalToHexadecimal.java index 2cefc92002ca..dd8d877109b5 100644 --- a/Conversions/OctalToHexadecimal.java +++ b/Conversions/OctalToHexadecimal.java @@ -59,6 +59,7 @@ public static void main(String args[]) { // Pass the decimla number to function and get converted Hex form of the number String hex = DecimalToHex(decimal); System.out.println("The Hexadecimal equivalant is: " + hex); + input.close(); } } diff --git a/Conversions/RomanToInteger.java b/Conversions/RomanToInteger.java index 7d6e0650ab46..4563ce4a4a72 100644 --- a/Conversions/RomanToInteger.java +++ b/Conversions/RomanToInteger.java @@ -13,6 +13,7 @@ public class RomanToInteger { put('D', 500); put('M', 1000); }}; + //Roman Number = Roman Numerals /** * This function convert Roman number into Integer diff --git a/DIRECTORY.md b/DIRECTORY.md new file mode 100644 index 000000000000..a8052bc883b7 --- /dev/null +++ b/DIRECTORY.md @@ -0,0 +1,202 @@ + +## ciphers + * [AES](https://github.com/TheAlgorithms/Java/blob/master/ciphers/AES.java) + * [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/ciphers/AESEncryption.java) + * [Caesar](https://github.com/TheAlgorithms/Java/blob/master/ciphers/Caesar.java) + * [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/ciphers/ColumnarTranspositionCipher.java) + * [RSA](https://github.com/TheAlgorithms/Java/blob/master/ciphers/RSA.java) + * [SimpleSubstitutionCipher](https://github.com/TheAlgorithms/Java/blob/master/ciphers/SimpleSubstitutionCipher.java) + * [Vigenere](https://github.com/TheAlgorithms/Java/blob/master/ciphers/Vigenere.java) + +## Conversions + * [AnyBaseToAnyBase](https://github.com/TheAlgorithms/Java/blob/master/Conversions/AnyBaseToAnyBase.java) + * [AnyBaseToDecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/AnyBaseToDecimal.java) + * [AnytoAny](https://github.com/TheAlgorithms/Java/blob/master/Conversions/AnytoAny.java) + * [BinaryToDecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/BinaryToDecimal.java) + * [BinaryToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/BinaryToHexadecimal.java) + * [BinaryToOctal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/BinaryToOctal.java) + * [DecimalToAnyBase](https://github.com/TheAlgorithms/Java/blob/master/Conversions/DecimalToAnyBase.java) + * [DecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/Conversions/DecimalToBinary.java) + * [DecimalToHexaDecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/DecimalToHexaDecimal.java) + * [DecimalToOctal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/DecimalToOctal.java) + * [HexaDecimalToBinary](https://github.com/TheAlgorithms/Java/blob/master/Conversions/HexaDecimalToBinary.java) + * [HexaDecimalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/HexaDecimalToDecimal.java) + * [HexToOct](https://github.com/TheAlgorithms/Java/blob/master/Conversions/HexToOct.java) + * [IntegerToRoman](https://github.com/TheAlgorithms/Java/blob/master/Conversions/IntegerToRoman.java) + * [OctalToDecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/OctalToDecimal.java) + * [OctalToHexadecimal](https://github.com/TheAlgorithms/Java/blob/master/Conversions/OctalToHexadecimal.java) + * [RomanToInteger](https://github.com/TheAlgorithms/Java/blob/master/Conversions/RomanToInteger.java) + +## DataStructures + * Bags + * [Bag](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Bags/Bag.java) + * Buffers + * [CircularBuffer](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Buffers/CircularBuffer.java) + * Graphs + * [BellmanFord](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/BellmanFord.java) + * [ConnectedComponent](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/ConnectedComponent.java) + * [Cycles](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/Cycles.java) + * [FloydWarshall](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/FloydWarshall.java) + * [Graphs](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/Graphs.java) + * [MatrixGraphs](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/MatrixGraphs.java) + * [PrimMST](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Graphs/PrimMST.java) + * HashMap + * Hashing + * [HashMap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/HashMap.java) + * [LinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/LinkedList.java) + * [Main](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/Main.java) + * [Node](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/HashMap/Hashing/Node.java) + * Heaps + * [EmptyHeapException](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/EmptyHeapException.java) + * [Heap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/Heap.java) + * [HeapElement](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/HeapElement.java) + * [MaxHeap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MaxHeap.java) + * [MinHeap](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MinHeap.java) + * [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MinPriorityQueue.java) + * Lists + * [CircleLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CircleLinkedList.java) + * [CursorLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CursorLinkedList.java) + * [DoublyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/DoublyLinkedList.java) + * [Merge K SortedLinkedlist](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/Merge_K_SortedLinkedlist.java) + * [MergeSortedArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedArrayList.java) + * [SinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java) + * Queues + * [GenericArrayListQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java) + * [LinkedQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/LinkedQueue.java) + * [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/PriorityQueues.java) + * [Queues](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/Queues.java) + * Stacks + * [BalancedBrackets](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/BalancedBrackets.java) + * [DecimalToAnyUsingStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/DecimalToAnyUsingStack.java) + * [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/NodeStack.java) + * [StackArray](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArray.java) + * [StackArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackArrayList.java) + * [StackOfLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Stacks/StackOfLinkedList.java) + * Trees + * [AVLTree](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/AVLTree.java) + * [BinaryTree](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/BinaryTree.java) + * [GenericTree](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/GenericTree.java) + * [LevelOrderTraversal](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/LevelOrderTraversal.java) + * [LevelOrderTraversalQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/LevelOrderTraversalQueue.java) + * [PrintTopViewofTree](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/PrintTopViewofTree.java) + * [RedBlackBST](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/RedBlackBST.java) + * [TreeTraversal](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/TreeTraversal.java) + * [TrieImp](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/TrieImp.java) + * [ValidBSTOrNot](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Trees/ValidBSTOrNot.java) + +## divideconquer + * [ClosestPair](https://github.com/TheAlgorithms/Java/blob/master/divideconquer/ClosestPair.java) + * [SkylineAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/divideconquer/SkylineAlgorithm.java) + +## DynamicProgramming + * [CoinChange](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/CoinChange.java) + * [EditDistance](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/EditDistance.java) + * [EggDropping](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/EggDropping.java) + * [Fibonacci](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/Fibonacci.java) + * [FordFulkerson](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/FordFulkerson.java) + * [KadaneAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/KadaneAlgorithm.java) + * [Knapsack](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/Knapsack.java) + * [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/LevenshteinDistance.java) + * [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/LongestCommonSubsequence.java) + * [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/LongestIncreasingSubsequence.java) + * [LongestValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/LongestValidParentheses.java) + * [MatrixChainMultiplication](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/MatrixChainMultiplication.java) + * [RodCutting](https://github.com/TheAlgorithms/Java/blob/master/DynamicProgramming/RodCutting.java) + +## Maths + * [AbsoluteMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteMax.java) + * [AbsoluteMin](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteMin.java) + * [AbsoluteValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/AbsoluteValue.java) + * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/Maths/Factorial.java) + * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FactorialRecursion.java) + * [FibonacciNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/FibonacciNumber.java) + * [FindMax](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMax.java) + * [FindMaxRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMaxRecursion.java) + * [FindMin](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMin.java) + * [FindMinRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/FindMinRecursion.java) + * [GCD](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCD.java) + * [GCDRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/GCDRecursion.java) + * [MaxValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MaxValue.java) + * [MinValue](https://github.com/TheAlgorithms/Java/blob/master/Maths/MinValue.java) + * [PalindromeNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PalindromeNumber.java) + * [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/Maths/ParseInteger.java) + * [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/Maths/PerfectNumber.java) + * [Pow](https://github.com/TheAlgorithms/Java/blob/master/Maths/Pow.java) + * [PowRecursion](https://github.com/TheAlgorithms/Java/blob/master/Maths/PowRecursion.java) + * [PrimeCheck](https://github.com/TheAlgorithms/Java/blob/master/Maths/PrimeCheck.java) + +## MinimizingLateness + * [MinimizingLateness](https://github.com/TheAlgorithms/Java/blob/master/MinimizingLateness/MinimizingLateness.java) + +## Misc + * [heap sort](https://github.com/TheAlgorithms/Java/blob/master/Misc/heap_sort.java) + * [MedianOfRunningArray](https://github.com/TheAlgorithms/Java/blob/master/Misc/MedianOfRunningArray.java) + * [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/Misc/PalindromePrime.java) + +## Others + * [Abecedarian](https://github.com/TheAlgorithms/Java/blob/master/Others/Abecedarian.java) + * [Armstrong](https://github.com/TheAlgorithms/Java/blob/master/Others/Armstrong.java) + * [BestFit](https://github.com/TheAlgorithms/Java/blob/master/Others/BestFit.java) + * [BrianKernighanAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Others/BrianKernighanAlgorithm.java) + * [CountChar](https://github.com/TheAlgorithms/Java/blob/master/Others/CountChar.java) + * [CountWords](https://github.com/TheAlgorithms/Java/blob/master/Others/CountWords.java) + * [CRC32](https://github.com/TheAlgorithms/Java/blob/master/Others/CRC32.java) + * [CRCAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Others/CRCAlgorithm.java) + * [Dijkstra](https://github.com/TheAlgorithms/Java/blob/master/Others/Dijkstra.java) + * [EulersFunction](https://github.com/TheAlgorithms/Java/blob/master/Others/EulersFunction.java) + * [FibToN](https://github.com/TheAlgorithms/Java/blob/master/Others/FibToN.java) + * [FirstFit](https://github.com/TheAlgorithms/Java/blob/master/Others/FirstFit.java) + * [FloydTriangle](https://github.com/TheAlgorithms/Java/blob/master/Others/FloydTriangle.java) + * [GuassLegendre](https://github.com/TheAlgorithms/Java/blob/master/Others/GuassLegendre.java) + * [InsertDeleteInArray](https://github.com/TheAlgorithms/Java/blob/master/Others/InsertDeleteInArray.java) + * [KMP](https://github.com/TheAlgorithms/Java/blob/master/Others/KMP.java) + * [Krishnamurthy](https://github.com/TheAlgorithms/Java/blob/master/Others/Krishnamurthy.java) + * [LinearCongruentialGenerator](https://github.com/TheAlgorithms/Java/blob/master/Others/LinearCongruentialGenerator.java) + * [LowestBasePalindrome](https://github.com/TheAlgorithms/Java/blob/master/Others/LowestBasePalindrome.java) + * [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/Others/Palindrome.java) + * [PasswordGen](https://github.com/TheAlgorithms/Java/blob/master/Others/PasswordGen.java) + * [PerlinNoise](https://github.com/TheAlgorithms/Java/blob/master/Others/PerlinNoise.java) + * [PowerOfTwoOrNot](https://github.com/TheAlgorithms/Java/blob/master/Others/PowerOfTwoOrNot.java) + * [QueueUsingTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/Others/QueueUsingTwoStacks.java) + * [RemoveDuplicateFromString](https://github.com/TheAlgorithms/Java/blob/master/Others/RemoveDuplicateFromString.java) + * [ReturnSubsequence](https://github.com/TheAlgorithms/Java/blob/master/Others/ReturnSubsequence.java) + * [ReverseStackUsingRecursion](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseStackUsingRecursion.java) + * [ReverseString](https://github.com/TheAlgorithms/Java/blob/master/Others/ReverseString.java) + * [RootPrecision](https://github.com/TheAlgorithms/Java/blob/master/Others/RootPrecision.java) + * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Java/blob/master/Others/SieveOfEratosthenes.java) + * [SJF](https://github.com/TheAlgorithms/Java/blob/master/Others/SJF.java) + * [SkylineProblem](https://github.com/TheAlgorithms/Java/blob/master/Others/SkylineProblem.java) + * [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/Others/StackPostfixNotation.java) + * [TopKWords](https://github.com/TheAlgorithms/Java/blob/master/Others/TopKWords.java) + * [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/Others/TowerOfHanoi.java) + * [WorstFit](https://github.com/TheAlgorithms/Java/blob/master/Others/WorstFit.java) + +## Searches + * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) + * [InterpolationSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/InterpolationSearch.java) + * [IterativeBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/IterativeBinarySearch.java) + * [IterativeTernarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/IterativeTernarySearch.java) + * [JumpSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/JumpSearch.java) + * [LinearSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/LinearSearch.java) + * [SaddlebackSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/SaddlebackSearch.java) + * [SearchAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Searches/SearchAlgorithm.java) + * [TernarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/TernarySearch.java) + +## Sorts + * [BogoSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BogoSort.java) + * [BubbleSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/BubbleSort.java) + * [CocktailShakerSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CocktailShakerSort.java) + * [CombSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CombSort.java) + * [CountingSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CountingSort.java) + * [CycleSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/CycleSort.java) + * [GnomeSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/GnomeSort.java) + * [HeapSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/HeapSort.java) + * [InsertionSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/InsertionSort.java) + * [MergeSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/MergeSort.java) + * [PancakeSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/PancakeSort.java) + * [QuickSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/QuickSort.java) + * [RadixSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/RadixSort.java) + * [SelectionSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SelectionSort.java) + * [ShellSort](https://github.com/TheAlgorithms/Java/blob/master/Sorts/ShellSort.java) + * [SortAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SortAlgorithm.java) + * [SortUtils](https://github.com/TheAlgorithms/Java/blob/master/Sorts/SortUtils.java) diff --git a/DataStructures/Graphs/BellmanFord.java b/DataStructures/Graphs/BellmanFord.java index 717cb2803031..6dffede9e606 100644 --- a/DataStructures/Graphs/BellmanFord.java +++ b/DataStructures/Graphs/BellmanFord.java @@ -1,3 +1,5 @@ +package DataStructures.Graphs; + import java.util.*; class BellmanFord /*Implementation of Bellman ford to detect negative cycles. Graph accepts inputs in form of edges which have @@ -98,6 +100,7 @@ public void go()//Interactive run for understanding the class first time. Assume System.out.println(); } } + sc.close(); } /** * @param source Starting vertex diff --git a/DataStructures/HashMap/Hashing/Main.java b/DataStructures/HashMap/Hashing/Main.java index 66f65c1d9064..340f0760b2e5 100644 --- a/DataStructures/HashMap/Hashing/Main.java +++ b/DataStructures/HashMap/Hashing/Main.java @@ -42,6 +42,7 @@ public static void main(String[] args) { return; } } + In.close(); } } } \ No newline at end of file diff --git a/DataStructures/Queues/LinkedQueue.java b/DataStructures/Queues/LinkedQueue.java index 1c85937b9a06..dac13dcaaad6 100644 --- a/DataStructures/Queues/LinkedQueue.java +++ b/DataStructures/Queues/LinkedQueue.java @@ -1,4 +1,4 @@ -package DataStructures; +package DataStructures.Queues; import java.util.NoSuchElementException; diff --git a/DataStructures/Stacks/NodeStack.java b/DataStructures/Stacks/NodeStack.java index c598bb8fcba1..616e6a1e1548 100644 --- a/DataStructures/Stacks/NodeStack.java +++ b/DataStructures/Stacks/NodeStack.java @@ -1,3 +1,4 @@ +package DataStructures.Stacks; /** * Implementation of a stack using nodes. * Unlimited size, no arraylist. diff --git a/DataStructures/Stacks/StackArray.java b/DataStructures/Stacks/StackArray.java index 88da42cd3dcb..4365dc448d2d 100644 --- a/DataStructures/Stacks/StackArray.java +++ b/DataStructures/Stacks/StackArray.java @@ -1,3 +1,5 @@ +package DataStructures.Stacks; + /** * This class implements a Stack using a regular array. *
diff --git a/DataStructures/Stacks/StackArrayList.java b/DataStructures/Stacks/StackArrayList.java
index afc804440403..666b9ea99055 100644
--- a/DataStructures/Stacks/StackArrayList.java
+++ b/DataStructures/Stacks/StackArrayList.java
@@ -1,3 +1,5 @@
+package DataStructures.Stacks;
+
import java.util.ArrayList;
/**
diff --git a/DataStructures/Stacks/StackOfLinkedList.java b/DataStructures/Stacks/StackOfLinkedList.java
index 5a2b8fa08258..60d8a1b0f7a0 100644
--- a/DataStructures/Stacks/StackOfLinkedList.java
+++ b/DataStructures/Stacks/StackOfLinkedList.java
@@ -1,5 +1,7 @@
package DataStructures.Stacks;
+import java.util.NoSuchElementException;
+
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
diff --git a/DataStructures/Trees/BinaryTree.java b/DataStructures/Trees/BinaryTree.java
index 75d3af18e56b..6ef4b5ada325 100644
--- a/DataStructures/Trees/BinaryTree.java
+++ b/DataStructures/Trees/BinaryTree.java
@@ -145,12 +145,19 @@ else if (temp.left != null && temp.right != null) {
successor.left.parent = successor;
//If the successor has a right child, the child's grandparent is it's new parent
- if (successor.right != null && successor.parent != temp) {
- successor.right.parent = successor.parent;
- successor.parent.left = successor.right;
- successor.right = temp.right;
- successor.right.parent = successor;
+ if(successor.parent!=temp){
+ if(successor.right!=null){
+ successor.right.parent = successor.parent;
+ successor.parent.left = successor.right;
+ successor.right = temp.right;
+ successor.right.parent = successor;
+ }else{
+ successor.parent.left=null;
+ successor.right=temp.right;
+ successor.right.parent=successor;
+ }
}
+
if (temp == root) {
successor.parent = null;
root = successor;
diff --git a/DataStructures/Trees/RedBlackBST.java b/DataStructures/Trees/RedBlackBST.java
index 97a88e9a43de..197c96321975 100644
--- a/DataStructures/Trees/RedBlackBST.java
+++ b/DataStructures/Trees/RedBlackBST.java
@@ -309,6 +309,7 @@ public void insertDemo() {
printTreepre(root);
break;
}
+ scan.close();
}
public void deleteDemo() {
diff --git a/DynamicProgramming/EditDistance.java b/DynamicProgramming/EditDistance.java
index 210469b84108..20cb8803a754 100644
--- a/DynamicProgramming/EditDistance.java
+++ b/DynamicProgramming/EditDistance.java
@@ -74,5 +74,6 @@ public static void main(String[] args) {
//ans stores the final Edit Distance between the two strings
int ans = minDistance(s1, s2);
System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 + "\" is " + ans);
+ input.close();
}
}
diff --git a/DynamicProgramming/Fibonacci.java b/DynamicProgramming/Fibonacci.java
index 112a014a378a..89be0836f6af 100644
--- a/DynamicProgramming/Fibonacci.java
+++ b/DynamicProgramming/Fibonacci.java
@@ -22,6 +22,7 @@ public static void main(String[] args) {
System.out.println(fibMemo(n));
System.out.println(fibBotUp(n));
System.out.println(fibOptimized(n));
+ sc.close();
}
/**
diff --git a/DynamicProgramming/LongestIncreasingSubsequence.java b/DynamicProgramming/LongestIncreasingSubsequence.java
index 2b07e040db13..c8296518590d 100644
--- a/DynamicProgramming/LongestIncreasingSubsequence.java
+++ b/DynamicProgramming/LongestIncreasingSubsequence.java
@@ -17,6 +17,7 @@ public static void main(String[] args) {
}
System.out.println(LIS(ar));
+ sc.close();
}
private static int upperBound(int[] ar, int l, int r, int key) {
diff --git a/Maths/AbsoluteMax.java b/Maths/AbsoluteMax.java
index 18634bc3582f..f454f8ae003d 100644
--- a/Maths/AbsoluteMax.java
+++ b/Maths/AbsoluteMax.java
@@ -15,7 +15,7 @@ public static void main(String[] args) {
}
/**
- * get the value, it's absolute value is max
+ * get the value, return the absolute max value
*
* @param numbers contains elements
* @return the absolute max value
diff --git a/Maths/AbsoluteMin.java b/Maths/AbsoluteMin.java
index 4af43c5c08ac..576019581f9c 100644
--- a/Maths/AbsoluteMin.java
+++ b/Maths/AbsoluteMin.java
@@ -15,7 +15,7 @@ public static void main(String[] args) {
}
/**
- * get the value, it's absolute value is min
+ * get the value, returns the absolute min value min
*
* @param numbers contains elements
* @return the absolute min value
diff --git a/Maths/Factorial.java b/Maths/Factorial.java
index 1734e93fb83d..3feb43356129 100644
--- a/Maths/Factorial.java
+++ b/Maths/Factorial.java
@@ -1,25 +1,28 @@
package Maths;
+//change around 'n' for different factorial results
public class Factorial {
public static void main(String[] args) {
int n = 5;
System.out.println(n + "! = " + factorial(n));
}
+ //Factorial = n! = n1 * (n-1) * (n-2)*...1
+
/**
- * Calculate factorial
+ * Calculate factorial N
*
* @param n the number
* @return the factorial of {@code n}
*/
public static long factorial(int n) {
if (n < 0) {
- throw new ArithmeticException("n < 0");
+ throw new ArithmeticException("n < 0"); //Dont work with less than 0
}
long fac = 1;
for (int i = 1; i <= n; ++i) {
fac *= i;
}
- return fac;
+ return fac; //Return factorial
}
}
diff --git a/Maths/Pow.java b/Maths/Pow.java
index 605e01d98234..ac58fbbbe7cd 100644
--- a/Maths/Pow.java
+++ b/Maths/Pow.java
@@ -1,11 +1,12 @@
-package maths;
+package Maths;
+//POWER (exponentials) Examples (a^b)
public class Pow {
public static void main(String[] args) {
- assert pow(2, 0) == Math.pow(2, 0);
- assert pow(0, 2) == Math.pow(0, 2);
- assert pow(2, 10) == Math.pow(2, 10);
- assert pow(10, 2) == Math.pow(10, 2);
+ assert pow(2, 0) == Math.pow(2, 0); // == 1
+ assert pow(0, 2) == Math.pow(0, 2); // == 0
+ assert pow(2, 10) == Math.pow(2, 10); // == 1024
+ assert pow(10, 2) == Math.pow(10, 2); // == 100
}
/**
diff --git a/Maths/PrimeCheck.java b/Maths/PrimeCheck.java
index 3093894a3a3f..c972aa8c3e5a 100644
--- a/Maths/PrimeCheck.java
+++ b/Maths/PrimeCheck.java
@@ -13,6 +13,7 @@ public static void main(String[] args) {
} else {
System.out.println(n + " is not a prime number");
}
+ scanner.close();
}
/***
diff --git a/MinimizingLateness/MinimizingLateness.java b/MinimizingLateness/MinimizingLateness.java
index e5f71e9ef44a..1438f6f3dbcc 100644
--- a/MinimizingLateness/MinimizingLateness.java
+++ b/MinimizingLateness/MinimizingLateness.java
@@ -53,5 +53,6 @@ public static void main(String[] args) throws IOException {
System.out.println();
System.out.println("Output Data : ");
System.out.println(lateness);
+ in.close();
}
}
diff --git a/Misc/PalindromePrime.java b/Misc/PalindromePrime.java
index b13a23d83792..77a918c32297 100644
--- a/Misc/PalindromePrime.java
+++ b/Misc/PalindromePrime.java
@@ -9,6 +9,7 @@ public static void main(String[] args) { // Main funtion
System.out.println("Enter the quantity of First Palindromic Primes you want");
int n = in.nextInt(); // Input of how many first pallindromic prime we want
functioning(n); // calling function - functioning
+ in.close();
}
public static boolean prime(int num) { // checking if number is prime or not
diff --git a/Others/BestFit.java b/Others/BestFit.java
new file mode 100644
index 000000000000..71814d22b9f1
--- /dev/null
+++ b/Others/BestFit.java
@@ -0,0 +1,89 @@
+package Others;
+
+import java.util.ArrayList;
+
+/**
+ * @author Dekas Dimitrios
+ */
+public class BestFit {
+ private static final int NO_ALLOCATION = -255; // if a process has been allocated in position -255,
+ // it means that it has not been actually allocated.
+
+ /**
+ * Method to find the maximum valued element of an array filled with positive integers.
+ *
+ * @param array: an array filled with positive integers.
+ * @return the maximum valued element of the array.
+ */
+ private static int findMaxElement(int[] array) {
+ int max = -1;
+ for (int value : array) {
+ if (value > max) {
+ max = value;
+ }
+ }
+ return max;
+ }
+
+ /**
+ * Method to find the index of the memory block that is going to fit the given process based on the best fit algorithm.
+ *
+ * @param blocks: the array with the available memory blocks.
+ * @param process: the size of the process.
+ * @return the index of the block that fits, or -255 if no such block exists.
+ */
+ private static int findBestFit(int[] blockSizes, int processSize) {
+ // Initialize minDiff with an unreachable value by a difference between a blockSize and the processSize.
+ int minDiff = findMaxElement(blockSizes);
+ int index = NO_ALLOCATION; // If there is no block that can fit the process, return NO_ALLOCATION as the result.
+ for(int i=0 ; i < blockSizes.length ; i++) { // Find the most fitting memory block for the given process.
+ if(blockSizes[i] - processSize < minDiff && blockSizes[i] - processSize >= 0) {
+ minDiff = blockSizes[i] - processSize;
+ index = i;
+ }
+ }
+ return index;
+ }
+
+ /**
+ * Method to allocate memory to blocks according to the best fit
+ * algorithm. It should return an ArrayList of Integers, where the
+ * index is the process ID (zero-indexed) and the value is the block
+ * number (also zero-indexed).
+ *
+ * @param sizeOfBlocks: an int array that contains the sizes of the memory blocks available.
+ * @param sizeOfProcesses: an int array that contains the sizes of the processes we need memory blocks for.
+ * @return the ArrayList filled with Integers repressenting the memory allocation that took place.
+ */
+ static ArrayList