---
checkstyle.xml | 2 +-
.../AllPathsFromSourceToTarget.java | 14 ++--
.../graphs/HamiltonianCycle.java | 18 ++---
.../datastructures/graphs/KahnsAlgorithm.java | 3 -
.../datastructures/graphs/Kosaraju.java | 71 ++++++++---------
.../graphs/TarjansAlgorithm.java | 79 +++++++++----------
.../datastructures/lists/RandomNode.java | 60 ++++++--------
.../lists/SinglyLinkedList.java | 9 +--
.../trees/CeilInBinarySearchTree.java | 2 -
.../datastructures/trees/TrieImp.java | 6 +-
.../dynamicprogramming/CatalanNumber.java | 9 +--
.../CountFriendsPairing.java | 18 ++---
.../dynamicprogramming/EditDistance.java | 3 +-
.../dynamicprogramming/KadaneAlgorithm.java | 24 +++---
.../dynamicprogramming/NewManShanksPrime.java | 13 ++-
.../dynamicprogramming/RegexMatching.java | 2 -
.../dynamicprogramming/SubsetCount.java | 6 +-
.../maths/AutomorphicNumber.java | 6 +-
.../com/thealgorithms/maths/DigitalRoot.java | 20 ++---
.../thealgorithms/maths/FastInverseSqrt.java | 45 +++++------
.../com/thealgorithms/maths/FrizzyNumber.java | 11 +--
.../thealgorithms/maths/JosephusProblem.java | 3 -
.../thealgorithms/maths/PascalTriangle.java | 6 +-
.../others/BankersAlgorithm.java | 4 +-
.../com/thealgorithms/others/Dijkstra.java | 9 +--
.../others/MemoryManagementAlgorithms.java | 4 +-
.../com/thealgorithms/others/RabinKarp.java | 9 ++-
.../others/RotateMatrixBy90Degrees.java | 3 +-
.../com/thealgorithms/sorts/LinkListSort.java | 33 ++++----
.../com/thealgorithms/strings/Anagrams.java | 36 ++++-----
.../NumbersDifferentSignsTest.java | 10 +--
.../graphs/BoruvkaAlgorithmTest.java | 6 +-
.../lists/QuickSortLinkedListTest.java | 10 +--
.../lists/ReverseKGroupTest.java | 10 +--
.../lists/RotateSinglyLinkedListsTest.java | 9 +--
.../PreemptivePrioritySchedulingTest.java | 9 +--
.../thealgorithms/strings/WordLadderTest.java | 38 ++++-----
37 files changed, 273 insertions(+), 347 deletions(-)
diff --git a/checkstyle.xml b/checkstyle.xml
index 45431b39897a..af5e3527e32f 100644
--- a/checkstyle.xml
+++ b/checkstyle.xml
@@ -99,7 +99,7 @@
-
+
diff --git a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
index a21f8c05f292..6f93b704ffb2 100644
--- a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
+++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
@@ -1,16 +1,14 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To find all possible paths from source to destination*/
-
-/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
package com.thealgorithms.backtracking;
import java.util.ArrayList;
import java.util.List;
+/**
+ * Program description - To find all possible paths from source to destination
+ * Wikipedia
+ *
+ * @author Siddhant Swarup Mallick
+ */
public class AllPathsFromSourceToTarget {
// No. of vertices in graph
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java
index 65483eeeb65c..f12e3892b1b2 100644
--- a/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java
+++ b/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java
@@ -2,9 +2,9 @@
/**
* Java program for Hamiltonian Cycle
- * (https://en.wikipedia.org/wiki/Hamiltonian_path)
+ * wikipedia
*
- * @author Akshay Dubey (https://github.com/itsAkshayDubey)
+ * @author Akshay Dubey
*/
public class HamiltonianCycle {
@@ -58,31 +58,31 @@ public boolean isPathFound(int vertex) {
return true;
}
- /** all vertices selected but last vertex not linked to 0 **/
+ /* all vertices selected but last vertex not linked to 0 **/
if (this.pathCount == this.vertex) {
return false;
}
for (int v = 0; v < this.vertex; v++) {
- /** if connected **/
+ /* if connected **/
if (this.graph[vertex][v] == 1) {
- /** add to path **/
+ /* add to path **/
this.cycle[this.pathCount++] = v;
- /** remove connection **/
+ /* remove connection **/
this.graph[vertex][v] = 0;
this.graph[v][vertex] = 0;
- /** if vertex not already selected solve recursively **/
+ /* if vertex not already selected solve recursively **/
if (!isPresent(v)) {
return isPathFound(v);
}
- /** restore connection **/
+ /* restore connection **/
this.graph[vertex][v] = 1;
this.graph[v][vertex] = 1;
- /** remove path **/
+ /* remove path **/
this.cycle[--this.pathCount] = -1;
}
}
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java
index be83ea32f496..d5035cf625a6 100644
--- a/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java
+++ b/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java
@@ -8,9 +8,6 @@
import java.util.Queue;
import java.util.Set;
-/**
- * An algorithm that sorts a graph in toplogical order.
- */
/**
* A class that represents the adjaceny list of a graph
*/
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java
index 7c0c0b2bee78..c5f15839f997 100644
--- a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java
+++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java
@@ -6,53 +6,50 @@
/**
* Java program that implements Kosaraju Algorithm.
- * @author Shivanagouda S A (https://github.com/shivu2002a)
- *
- */
-
-/**
+ * @author Shivanagouda S A
+ *
* Kosaraju algorithm is a linear time algorithm to find the strongly connected components of a
- directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
- transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
- graph.
+directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
+transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
+graph.
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
- The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
- connected. Single node is always a SCC.
+The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
+connected. Single node is always a SCC.
* Example:
- 0 <--- 2 -------> 3 -------- > 4 ---- > 7
- | ^ | ^ ^
- | / | \ /
- | / | \ /
- v / v \ /
- 1 5 --> 6
+0 <--- 2 -------> 3 -------- > 4 ---- > 7
+| ^ | ^ ^
+| / | \ /
+| / | \ /
+v / v \ /
+1 5 --> 6
- For the above graph, the SCC list goes as follows:
- 0, 1, 2
- 3
- 4, 5, 6
- 7
+For the above graph, the SCC list goes as follows:
+0, 1, 2
+3
+4, 5, 6
+7
- We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
+We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
- {@summary}
+{@summary}
* Kosaraju Algorithm:
- 1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
- sorted by lowest finish time.
- 2. Find the transpose graph by reversing the edges.
- 3. Pop nodes one by one from the stack and again to DFS on the modified graph.
-
- The transpose graph of the above graph:
- 0 ---> 2 <------- 3 <------- 4 <------ 7
- ^ / ^ \ /
- | / | \ /
- | / | \ /
- | v | v v
- 1 5 <--- 6
-
- We can observe that this graph has the same SCC as that of original graph.
+1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
+sorted by lowest finish time.
+2. Find the transpose graph by reversing the edges.
+3. Pop nodes one by one from the stack and again to DFS on the modified graph.
+
+The transpose graph of the above graph:
+0 ---> 2 <------- 3 <------- 4 <------ 7
+^ / ^ \ /
+| / | \ /
+| / | \ /
+| v | v v
+1 5 <--- 6
+
+We can observe that this graph has the same SCC as that of original graph.
*/
diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java
index 336e375f7d4e..de50044256c6 100644
--- a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java
+++ b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java
@@ -6,51 +6,48 @@
/**
* Java program that implements Tarjan's Algorithm.
- * @author Shivanagouda S A (https://github.com/shivu2002a)
- *
- */
-
-/**
+ * @author Shivanagouda S A
+ *
* Tarjan's algorithm is a linear time algorithm to find the strongly connected components of a
- directed graph, which, from here onwards will be referred as SCC.
+directed graph, which, from here onwards will be referred as SCC.
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
- The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
- connected. Single node is always a SCC.
+The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
+connected. Single node is always a SCC.
* Example:
- 0 --------> 1 -------> 3 --------> 4
- ^ /
- | /
- | /
- | /
- | /
- | /
- | /
- | /
- | /
- | /
- |V
- 2
-
- For the above graph, the SCC list goes as follows:
- 1, 2, 0
- 3
- 4
-
- We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
-
- {@summary}
- Tarjan's Algorithm:
- * DFS search produces a DFS tree
- * Strongly Connected Components form subtrees of the DFS tree.
- * If we can find the head of these subtrees, we can get all the nodes in that subtree (including
- the head) and that will be one SCC.
- * There is no back edge from one SCC to another (here can be cross edges, but they will not be
- used).
-
- * Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
- algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
+0 --------> 1 -------> 3 --------> 4
+^ /
+| /
+| /
+| /
+| /
+| /
+| /
+| /
+| /
+| /
+|V
+2
+
+For the above graph, the SCC list goes as follows:
+1, 2, 0
+3
+4
+
+We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
+
+{@summary}
+Tarjan's Algorithm:
+ * DFS search produces a DFS tree
+ * Strongly Connected Components form subtrees of the DFS tree.
+ * If we can find the head of these subtrees, we can get all the nodes in that subtree (including
+the head) and that will be one SCC.
+ * There is no back edge from one SCC to another (here can be cross edges, but they will not be
+used).
+
+ * Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
+algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
*/
public class TarjansAlgorithm {
@@ -58,7 +55,7 @@ public class TarjansAlgorithm {
// Timer for tracking lowtime and insertion time
private int time;
- private List> sccList = new ArrayList>();
+ private final List> sccList = new ArrayList>();
public List> stronglyConnectedComponents(int v, List> graph) {
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java
index 7318b8027f8c..dac88dd9f241 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/RandomNode.java
@@ -1,43 +1,37 @@
-/**
- * Author : Suraj Kumar
- * Github : https://github.com/skmodi649
- */
+package com.thealgorithms.datastructures.lists;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
/**
+ * @author Suraj Kumar
+ *
* PROBLEM DESCRIPTION :
* There is a single linked list and we are supposed to find a random node in the given linked list
- */
-
-/**
+ *
* ALGORITHM :
* Step 1 : START
* Step 2 : Create an arraylist of type integer
* Step 3 : Declare an integer type variable for size and linked list type for head
* Step 4 : We will use two methods, one for traversing through the linked list using while loop and
* also increase the size by 1
- *
+ *
* (a) RandomNode(head)
* (b) run a while loop till null;
* (c) add the value to arraylist;
* (d) increase the size;
- *
+ *
* Step 5 : Now use another method for getting random values using Math.random() and return the
* value present in arraylist for the calculated index Step 6 : Now in main() method we will simply
* insert nodes in the linked list and then call the appropriate method and then print the random
* node generated Step 7 : STOP
*/
-
-package com.thealgorithms.datastructures.lists;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Random;
-
public class RandomNode {
- private List list;
+ private final List list;
private int size;
- private static Random rand = new Random();
+ private static final Random RAND = new Random();
static class ListNode {
@@ -63,10 +57,23 @@ public RandomNode(ListNode head) {
}
public int getRandom() {
- int index = rand.nextInt(size);
+ int index = RAND.nextInt(size);
return list.get(index);
}
+ /**
+ * OUTPUT :
+ * First output :
+ * Random Node : 25
+ * Second output :
+ * Random Node : 78
+ * Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ * Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ * Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ */
// Driver program to test above functions
public static void main(String[] args) {
ListNode head = new ListNode(15);
@@ -80,18 +87,3 @@ public static void main(String[] args) {
System.out.println("Random Node : " + randomNum);
}
}
-/**
- * OUTPUT :
- * First output :
- * Random Node : 25
- * Second output :
- * Random Node : 78
- * Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- * Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- */
-/**
- * Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- */
diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
index bca3c77f2724..d5d3f31f4b66 100644
--- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
+++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java
@@ -5,7 +5,7 @@
import java.util.StringJoiner;
/**
- * https://en.wikipedia.org/wiki/Linked_list
+ * wikipedia
*/
public class SinglyLinkedList implements Iterable {
@@ -95,7 +95,7 @@ public void swapNodes(int valueFirst, int valueSecond) {
previousB = currentB;
currentB = currentB.next;
}
- /** If either of 'a' or 'b' is not present, then return */
+ /* If either of 'a' or 'b' is not present, then return */
if (currentA == null || currentB == null) {
return;
}
@@ -334,11 +334,6 @@ public void insertNth(int data, int position) {
size++;
}
- /**
- * Swaps nodes of two given values a and b.
- *
- */
-
/**
* Deletes a node at the head
*/
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/CeilInBinarySearchTree.java b/src/main/java/com/thealgorithms/datastructures/trees/CeilInBinarySearchTree.java
index f238b5e9fe8d..214e111b9f1a 100644
--- a/src/main/java/com/thealgorithms/datastructures/trees/CeilInBinarySearchTree.java
+++ b/src/main/java/com/thealgorithms/datastructures/trees/CeilInBinarySearchTree.java
@@ -18,8 +18,6 @@
*
* Ex.2. [30,20,40,10,25,35,50] represents level order traversal of a binary
* search tree. Find ceil for 52 Answer: -1
- */
-/**
*
* Solution 1: Brute Force Solution: Do an inorder traversal and save result
* into an array. Iterate over the array to get an element equal to or greater
diff --git a/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java b/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java
index 79c66cb90f01..d166653ff1b4 100644
--- a/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java
+++ b/src/main/java/com/thealgorithms/datastructures/trees/TrieImp.java
@@ -1,12 +1,12 @@
package com.thealgorithms.datastructures.trees;
+import java.util.Scanner;
+
/**
* Trie Data structure implementation without any libraries
*
- * @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
+ * @author Dheeraj Kumar Barnwal
*/
-import java.util.Scanner;
-
public class TrieImp {
public class TrieNode {
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java b/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java
index 8658bca3df52..d01066f611bd 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/CatalanNumber.java
@@ -1,15 +1,14 @@
package com.thealgorithms.dynamicprogramming;
+import java.util.Scanner;
/**
* This file contains an implementation of finding the nth CATALAN NUMBER using
- * dynamic programming Wikipedia: https://en.wikipedia.org/wiki/Catalan_number
+ * dynamic programming : Wikipedia
*
* Time Complexity: O(n^2) Space Complexity: O(n)
*
- * @author AMRITESH ANAND (https://github.com/amritesh19)
+ * @author AMRITESH ANAND
*/
-import java.util.Scanner;
-
public final class CatalanNumber {
private CatalanNumber() {
}
@@ -31,7 +30,7 @@ static long findNthCatalan(int n) {
catalanArray[0] = 1;
catalanArray[1] = 1;
- /**
+ /*
* The Catalan numbers satisfy the recurrence relation C₀=1 and Cn = Σ
* (Ci * Cn-1-i), i = 0 to n-1 , n > 0
*/
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java
index 8c70c9c3fada..e731524d4958 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/CountFriendsPairing.java
@@ -1,20 +1,14 @@
+package com.thealgorithms.dynamicprogramming;
+
/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-/**
+ * @author Siddhant Swarup Mallick
+
* In mathematics, the Golomb sequence is a non-decreasing integer sequence where n-th term is equal
* to number of times n appears in the sequence.
- */
-/**
- * Wikipedia Link - https://en.wikipedia.org/wiki/Golomb_sequence
+ * Wikipedia
+ * Program description - To find the Golomb sequence upto n
*/
-
-/** Program description - To find the Golomb sequence upto n */
-
-package com.thealgorithms.dynamicprogramming;
-
public final class CountFriendsPairing {
private CountFriendsPairing() {
}
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/EditDistance.java b/src/main/java/com/thealgorithms/dynamicprogramming/EditDistance.java
index 6db30514db68..55ce50d30ea4 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/EditDistance.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/EditDistance.java
@@ -1,5 +1,6 @@
package com.thealgorithms.dynamicprogramming;
+import java.util.Scanner;
/**
* A DynamicProgramming based solution for Edit Distance problem In Java
* Description of Edit Distance with an Example:
@@ -22,8 +23,6 @@
*
* @author SUBHAM SANGHAI
*/
-import java.util.Scanner;
-
public final class EditDistance {
private EditDistance() {
}
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java
index 9962cca217a0..905815d10a29 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/KadaneAlgorithm.java
@@ -1,15 +1,20 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To find the maximum subarray sum */
package com.thealgorithms.dynamicprogramming;
+/**
+ * @author Siddhant Swarup Mallick
+ * Program description - To find the maximum subarray sum
+ */
public final class KadaneAlgorithm {
private KadaneAlgorithm() {
}
+ /**
+ * OUTPUT :
+ * Input - {89,56,98,123,26,75,12,40,39,68,91}
+ * Output: it returns either true or false
+ * 1st approach Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ */
public static boolean maxSum(int[] a, int predictedAnswer) {
int sum = a[0];
int runningSum = 0;
@@ -28,11 +33,4 @@ public static boolean maxSum(int[] a, int predictedAnswer) {
// It returns true if sum and predicted answer matches
// The predicted answer is the answer itself. So it always return true
}
- /**
- * OUTPUT :
- * Input - {89,56,98,123,26,75,12,40,39,68,91}
- * Output: it returns either true or false
- * 1st approach Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- */
}
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
index 5d31d40dacdc..d15ea1f78a78 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java
@@ -1,13 +1,10 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To find the New Man Shanks Prime. */
-/** Wikipedia Link - https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime */
-
package com.thealgorithms.dynamicprogramming;
+/**
+ * @author Siddhant Swarup Mallick
+ * Program description - To find the New Man Shanks Prime.
+ * Wikipedia
+ */
public final class NewManShanksPrime {
private NewManShanksPrime() {
}
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java b/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java
index 43427457e307..c07563ad0020 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/RegexMatching.java
@@ -6,8 +6,6 @@
* cover the entire text ?-> matches single characters *-> match the sequence of
* characters
*
- */
-/**
* For calculation of Time and Space Complexity. Let N be length of src and M be
* length of pat
*
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
index cbe3ccae4ac7..0c5bc2c5884d 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
@@ -3,8 +3,8 @@
/**
* Find the number of subsets present in the given array with a sum equal to target.
* Based on Solution discussed on
- * StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
- * @author Samrat Podder(https://github.com/samratpodder)
+ * StackOverflow
+ * @author Samrat Podder
*/
public final class SubsetCount {
private SubsetCount() {
@@ -19,7 +19,7 @@ private SubsetCount() {
*
*/
public static int getCount(int[] arr, int target) {
- /**
+ /*
* Base Cases - If target becomes zero, we have reached the required sum for the subset
* If we reach the end of the array arr then, either if target==arr[end], then we add one to
* the final count Otherwise we add 0 to the final count
diff --git a/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java b/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
index 03c8a8989bb2..31f81da63f03 100644
--- a/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
+++ b/src/main/java/com/thealgorithms/maths/AutomorphicNumber.java
@@ -1,15 +1,13 @@
package com.thealgorithms.maths;
+import java.math.BigInteger;
/**
- * Wikipedia link for Automorphic Number : https://en.wikipedia.org/wiki/Automorphic_number
+ * Automorphic Number
* A number is said to be an Automorphic, if it is present in the last digit(s)
* of its square. Example- Let the number be 25, its square is 625. Since,
* 25(The input number) is present in the last two digits of its square(625), it
* is an Automorphic Number.
*/
-
-import java.math.BigInteger;
-
public final class AutomorphicNumber {
private AutomorphicNumber() {
}
diff --git a/src/main/java/com/thealgorithms/maths/DigitalRoot.java b/src/main/java/com/thealgorithms/maths/DigitalRoot.java
index 84b33f34c393..e8f5305c7569 100644
--- a/src/main/java/com/thealgorithms/maths/DigitalRoot.java
+++ b/src/main/java/com/thealgorithms/maths/DigitalRoot.java
@@ -1,8 +1,7 @@
+package com.thealgorithms.maths;
+
/**
- * Author : Suraj Kumar Modi
- * https://github.com/skmodi649
- */
-/**
+ * @author Suraj Kumar Modi
* You are given a number n. You need to find the digital root of n.
* DigitalRoot of a number is the recursive sum of its digits until we get a single digit number.
*
@@ -20,8 +19,6 @@
* which is not a single digit number, hence
* sum of digit of 45 is 9 which is a single
* digit number.
- */
-/**
* Algorithm :
* Step 1 : Define a method digitalRoot(int n)
* Step 2 : Define another method single(int n)
@@ -38,8 +35,6 @@
* Step 5 : In main method simply take n as input and then call digitalRoot(int n) function and
* print the result
*/
-package com.thealgorithms.maths;
-
final class DigitalRoot {
private DigitalRoot() {
}
@@ -53,6 +48,11 @@ public static int digitalRoot(int n) {
}
}
+ /**
+ * Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
+ * O(Number of Digits) Constraints: 1 <= n <= 10^7
+ */
+
// This function is used for finding the sum of the digits of number
public static int single(int n) {
if (n <= 9) { // if n becomes less than 10 than return n
@@ -63,7 +63,3 @@ public static int single(int n) {
} // n / 10 is the number obtained after removing the digit one by one
// The Sum of digits is stored in the Stack memory and then finally returned
}
-/**
- * Time Complexity: O((Number of Digits)^2) Auxiliary Space Complexity:
- * O(Number of Digits) Constraints: 1 <= n <= 10^7
- */
diff --git a/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
index a0dbfb1f70a4..01a52b913d30 100644
--- a/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
+++ b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
@@ -1,18 +1,26 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To find out the inverse square root of the given number*/
-
-/** Wikipedia Link - https://en.wikipedia.org/wiki/Fast_inverse_square_root */
-
package com.thealgorithms.maths;
+/**
+ * @author Siddhant Swarup Mallick
+ * Program description - To find out the inverse square root of the given number
+ * Wikipedia
+ */
public final class FastInverseSqrt {
private FastInverseSqrt() {
}
-
+ /**
+ * Returns the inverse square root of the given number upto 6 - 8 decimal places.
+ * calculates the inverse square root of the given number and returns true if calculated answer
+ * matches with given answer else returns false
+ *
+ * OUTPUT :
+ * Input - number = 4522
+ * Output: it calculates the inverse squareroot of a number and returns true with it matches the
+ * given answer else returns false. 1st approach Time Complexity : O(1) Auxiliary Space Complexity :
+ * O(1) Input - number = 4522 Output: it calculates the inverse squareroot of a number and returns
+ * true with it matches the given answer else returns false. 2nd approach Time Complexity : O(1)
+ * Auxiliary Space Complexity : O(1)
+ */
public static boolean inverseSqrt(float number) {
float x = number;
float xhalf = 0.5f * x;
@@ -24,11 +32,10 @@ public static boolean inverseSqrt(float number) {
}
/**
- * Returns the inverse square root of the given number upto 6 - 8 decimal places.
+ * Returns the inverse square root of the given number upto 14 - 16 decimal places.
* calculates the inverse square root of the given number and returns true if calculated answer
* matches with given answer else returns false
*/
-
public static boolean inverseSqrt(double number) {
double x = number;
double xhalf = 0.5d * x;
@@ -41,18 +48,4 @@ public static boolean inverseSqrt(double number) {
x *= number;
return x == 1 / Math.sqrt(number);
}
- /**
- * Returns the inverse square root of the given number upto 14 - 16 decimal places.
- * calculates the inverse square root of the given number and returns true if calculated answer
- * matches with given answer else returns false
- */
}
-/**
- * OUTPUT :
- * Input - number = 4522
- * Output: it calculates the inverse squareroot of a number and returns true with it matches the
- * given answer else returns false. 1st approach Time Complexity : O(1) Auxiliary Space Complexity :
- * O(1) Input - number = 4522 Output: it calculates the inverse squareroot of a number and returns
- * true with it matches the given answer else returns false. 2nd approach Time Complexity : O(1)
- * Auxiliary Space Complexity : O(1)
- */
diff --git a/src/main/java/com/thealgorithms/maths/FrizzyNumber.java b/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
index 3ae5e021df1b..ff2b00c26ce2 100644
--- a/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
+++ b/src/main/java/com/thealgorithms/maths/FrizzyNumber.java
@@ -1,12 +1,9 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To find the FrizzyNumber*/
-
package com.thealgorithms.maths;
+/**
+ * @author Siddhant Swarup Mallick
+ * Program description - To find the FrizzyNumber
+ */
public final class FrizzyNumber {
private FrizzyNumber() {
}
diff --git a/src/main/java/com/thealgorithms/maths/JosephusProblem.java b/src/main/java/com/thealgorithms/maths/JosephusProblem.java
index 7d19623b3ed0..98d839011a7f 100644
--- a/src/main/java/com/thealgorithms/maths/JosephusProblem.java
+++ b/src/main/java/com/thealgorithms/maths/JosephusProblem.java
@@ -5,9 +5,7 @@
* numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend
* brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings
* you to the 1st friend.
- */
-/**
The rules of the game are as follows:
1.Start at the 1st friend.
@@ -19,7 +17,6 @@
@author Kunal
*/
-
public final class JosephusProblem {
private JosephusProblem() {
}
diff --git a/src/main/java/com/thealgorithms/maths/PascalTriangle.java b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
index 95f92fbe1b49..9a9d4450cb98 100644
--- a/src/main/java/com/thealgorithms/maths/PascalTriangle.java
+++ b/src/main/java/com/thealgorithms/maths/PascalTriangle.java
@@ -37,17 +37,17 @@ private PascalTriangle() {
*/
public static int[][] pascal(int n) {
- /**
+ /*
* @param arr An auxiliary array to store generated pascal triangle values
* @return
*/
int[][] arr = new int[n][n];
- /**
+ /*
* @param line Iterate through every line and print integer(s) in it
* @param i Represents the column number of the element we are currently on
*/
for (int line = 0; line < n; line++) {
- /**
+ /*
* @Every line has number of integers equal to line number
*/
for (int i = 0; i <= line; i++) {
diff --git a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
index a22d7c737415..836526529374 100644
--- a/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
+++ b/src/main/java/com/thealgorithms/others/BankersAlgorithm.java
@@ -1,5 +1,7 @@
package com.thealgorithms.others;
+import java.util.Scanner;
+
/**
* This file contains an implementation of BANKER'S ALGORITM Wikipedia:
* https://en.wikipedia.org/wiki/Banker%27s_algorithm
@@ -18,8 +20,6 @@
*
* @author AMRITESH ANAND (https://github.com/amritesh19)
*/
-import java.util.Scanner;
-
public final class BankersAlgorithm {
private BankersAlgorithm() {
}
diff --git a/src/main/java/com/thealgorithms/others/Dijkstra.java b/src/main/java/com/thealgorithms/others/Dijkstra.java
index e2a778f8d6c3..a379100a2f3b 100644
--- a/src/main/java/com/thealgorithms/others/Dijkstra.java
+++ b/src/main/java/com/thealgorithms/others/Dijkstra.java
@@ -1,5 +1,9 @@
package com.thealgorithms.others;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.NavigableSet;
+import java.util.TreeSet;
/**
* Dijkstra's algorithm,is a graph search algorithm that solves the
* single-source shortest path problem for a graph with nonnegative edge path
@@ -15,11 +19,6 @@
* https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java Also most of the
* comments are from RosettaCode.
*/
-import java.util.HashMap;
-import java.util.Map;
-import java.util.NavigableSet;
-import java.util.TreeSet;
-
public final class Dijkstra {
private Dijkstra() {
}
diff --git a/src/main/java/com/thealgorithms/others/MemoryManagementAlgorithms.java b/src/main/java/com/thealgorithms/others/MemoryManagementAlgorithms.java
index 1f5f455f24e3..0924b8569942 100644
--- a/src/main/java/com/thealgorithms/others/MemoryManagementAlgorithms.java
+++ b/src/main/java/com/thealgorithms/others/MemoryManagementAlgorithms.java
@@ -1,11 +1,9 @@
package com.thealgorithms.others;
+import java.util.ArrayList;
/**
* @author Alexandros Lemonaris
*/
-
-import java.util.ArrayList;
-
public abstract class MemoryManagementAlgorithms {
/**
diff --git a/src/main/java/com/thealgorithms/others/RabinKarp.java b/src/main/java/com/thealgorithms/others/RabinKarp.java
index f8ca33becad3..cecf24e09b4a 100644
--- a/src/main/java/com/thealgorithms/others/RabinKarp.java
+++ b/src/main/java/com/thealgorithms/others/RabinKarp.java
@@ -1,12 +1,13 @@
package com.thealgorithms.others;
+import java.util.Scanner;
+
/**
* @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
+ *
+ An implementation of Rabin-Karp string matching algorithm
+ Program will simply end if there is no match
*/
-import java.util.Scanner;
-
-// An implementation of Rabin-Karp string matching algorithm
-// Program will simply end if there is no match
public final class RabinKarp {
private RabinKarp() {
}
diff --git a/src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java b/src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java
index 3930ca3e95ff..6ad0ef024342 100644
--- a/src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java
+++ b/src/main/java/com/thealgorithms/others/RotateMatrixBy90Degrees.java
@@ -1,11 +1,10 @@
package com.thealgorithms.others;
+import java.util.Scanner;
/**
* Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here
* is the algorithm for this problem .
*/
-import java.util.Scanner;
-
final class RotateMatrixBy90Degrees {
private RotateMatrixBy90Degrees() {
}
diff --git a/src/main/java/com/thealgorithms/sorts/LinkListSort.java b/src/main/java/com/thealgorithms/sorts/LinkListSort.java
index c9000f7e3778..bf8910d94eae 100644
--- a/src/main/java/com/thealgorithms/sorts/LinkListSort.java
+++ b/src/main/java/com/thealgorithms/sorts/LinkListSort.java
@@ -1,14 +1,10 @@
-/**
- * Author : Siddhant Swarup Mallick
- * Github : https://github.com/siddhant2002
- */
-
-/** Program description - To sort the LinkList as per sorting technique */
-
package com.thealgorithms.sorts;
import java.util.Arrays;
-
+/**
+ * @author Siddhant Swarup Mallick
+ * Program description - To sort the LinkList as per sorting technique
+ */
public class LinkListSort {
public static boolean isSorted(int[] p, int option) {
@@ -116,17 +112,6 @@ public static boolean isSorted(int[] p, int option) {
// Switch case is used to call the classes as per the user requirement
return false;
}
-
- boolean compare(int[] a, int[] b) {
- for (int i = 0; i < a.length; i++) {
- if (a[i] != b[i]) {
- return false;
- }
- }
- return true;
- // Both the arrays are checked for equalness. If both are equal then true is
- // returned else false is returned
- }
/**
* OUTPUT :
* Input - {89,56,98,123,26,75,12,40,39,68,91} is same for all the 3 classes
@@ -138,6 +123,16 @@ boolean compare(int[] a, int[] b) {
* 3rd approach Time Complexity : O(n logn)
* Auxiliary Space Complexity : O(n)
*/
+ boolean compare(int[] a, int[] b) {
+ for (int i = 0; i < a.length; i++) {
+ if (a[i] != b[i]) {
+ return false;
+ }
+ }
+ return true;
+ // Both the arrays are checked for equalness. If both are equal then true is
+ // returned else false is returned
+ }
}
class Node {
diff --git a/src/main/java/com/thealgorithms/strings/Anagrams.java b/src/main/java/com/thealgorithms/strings/Anagrams.java
index 106be5e1a596..f5e8fa84cd41 100644
--- a/src/main/java/com/thealgorithms/strings/Anagrams.java
+++ b/src/main/java/com/thealgorithms/strings/Anagrams.java
@@ -12,8 +12,24 @@
*/
public class Anagrams {
- // 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but
- // differ in running time.
+ /**
+ * 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but
+ * differ in running time.
+ * OUTPUT :
+ * first string ="deal" second string ="lead"
+ * Output: Anagram
+ * Input and output is constant for all four approaches
+ * 1st approach Time Complexity : O(n logn)
+ * Auxiliary Space Complexity : O(1)
+ * 2nd approach Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ * 3rd approach Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(1)
+ * 4th approach Time Complexity : O(n)
+ * Auxiliary Space Complexity : O(n)
+ * 5th approach Time Complexity: O(n)
+ * Auxiliary Space Complexity: O(1)
+ */
public static void main(String[] args) {
String first = "deal";
String second = "lead";
@@ -23,22 +39,6 @@ public static void main(String[] args) {
System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/
System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/
System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/
- /**
- * OUTPUT :
- * first string ="deal" second string ="lead"
- * Output: Anagram
- * Input and output is constant for all four approaches
- * 1st approach Time Complexity : O(n logn)
- * Auxiliary Space Complexity : O(1)
- * 2nd approach Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- * 3rd approach Time Complexity : O(n)
- * Auxiliary Space Complexity : O(1)
- * 4th approach Time Complexity : O(n)
- * Auxiliary Space Complexity : O(n)
- * 5th approach Time Complexity: O(n)
- * Auxiliary Space Complexity: O(1)
- */
}
boolean approach1(String s, String t) {
diff --git a/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java b/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java
index 13761ac23e44..f54070d39cdd 100644
--- a/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java
+++ b/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java
@@ -1,15 +1,13 @@
package com.thealgorithms.bitmanipulation;
-/**
- * test Cases of Numbers Different Signs
- * @author Bama Charan Chhandogi
- */
-
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
-
+/**
+ * test Cases of Numbers Different Signs
+ * @author Bama Charan Chhandogi
+ */
class NumbersDifferentSignsTest {
@Test
diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java
index 579e236699b3..8cd0b0a6838f 100644
--- a/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/graphs/BoruvkaAlgorithmTest.java
@@ -30,7 +30,7 @@ public void testBoruvkaMSTV9E14() {
edges.add(new BoruvkaAlgorithm.Edge(7, 8, 11));
final var graph = new Graph(9, edges);
- /**
+ /*
* Adjacency matrix
* 0 1 2 3 4 5 6 7 8
* 0 0 10 12 0 0 0 0 0 0
@@ -56,7 +56,7 @@ void testBoruvkaMSTV2E1() {
final var graph = new Graph(2, edges);
- /**
+ /*
* Adjacency matrix
* 0 1
* 0 0 10
@@ -79,7 +79,7 @@ void testCompleteGraphK4() {
final var graph = new Graph(4, edges);
- /**
+ /*
* Adjacency matrix
* 0 1 2 3
* 0 0 7 2 5
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java
index c4113b787de9..91e2ba5d43ce 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java
@@ -1,16 +1,14 @@
package com.thealgorithms.datastructures.lists;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.junit.jupiter.api.Test;
/**
* Test cases for QuickSortLinkedList
* Author: Prabhat-Kumar-42
* GitHub: https://github.com/Prabhat-Kumar-42
*/
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
-
-import org.junit.jupiter.api.Test;
-
public class QuickSortLinkedListTest {
@Test
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
index c03f5b14c641..e7e3cca4083f 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java
@@ -1,15 +1,13 @@
package com.thealgorithms.datastructures.lists;
-/**
- * Test cases for Reverse K Group LinkedList
- * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
- */
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
-
+/**
+ * Test cases for Reverse K Group LinkedList
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
public class ReverseKGroupTest {
@Test
diff --git a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
index d3c020f8881b..8b2ae424364e 100644
--- a/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
+++ b/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java
@@ -1,15 +1,14 @@
package com.thealgorithms.datastructures.lists;
-/**
- * Test cases for RotateSinglyLinkedLists
- * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
- */
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
+/**
+ * Test cases for RotateSinglyLinkedLists
+ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
+ */
public class RotateSinglyLinkedListsTest {
@Test
diff --git a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
index 61e2a2ac2690..1c8a25dfda49 100644
--- a/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
+++ b/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java
@@ -1,10 +1,5 @@
package com.thealgorithms.scheduling;
-/**
- * Test Cases of Preemptive Priority Scheduling Algorithm
- * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
- */
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
@@ -12,6 +7,10 @@
import java.util.List;
import org.junit.jupiter.api.Test;
+/**
+ * Test Cases of Preemptive Priority Scheduling Algorithm
+ * @author [Bama Charan Chhandogi](https://www.github.com/BamaCharanChhandogi)
+ */
class PreemptivePrioritySchedulingTest {
@Test
diff --git a/src/test/java/com/thealgorithms/strings/WordLadderTest.java b/src/test/java/com/thealgorithms/strings/WordLadderTest.java
index 4f1d07ebdc4a..c59f2d8b31be 100644
--- a/src/test/java/com/thealgorithms/strings/WordLadderTest.java
+++ b/src/test/java/com/thealgorithms/strings/WordLadderTest.java
@@ -8,30 +8,32 @@
public class WordLadderTest {
+ /**
+ * Test 1:
+ * Input: beginWord = "hit", endWord = "cog", wordList =
+ * ["hot","dot","dog","lot","log","cog"]
+ * Output: 5
+ * Explanation: One shortest transformation sequence is
+ * "hit" -> "hot" -> "dot" -> "dog" -> cog"
+ * which is 5 words long.
+ */
@Test
public void testWordLadder() {
- /**
- * Test 1:
- * Input: beginWord = "hit", endWord = "cog", wordList =
- * ["hot","dot","dog","lot","log","cog"]
- * Output: 5
- * Explanation: One shortest transformation sequence is
- * "hit" -> "hot" -> "dot" -> "dog" -> cog"
- * which is 5 words long.
- */
-
List wordList1 = Arrays.asList("hot", "dot", "dog", "lot", "log", "cog");
assertEquals(WordLadder.ladderLength("hit", "cog", wordList1), 5);
+ }
- /**
- * Test 2:
- * Input: beginWord = "hit", endWord = "cog", wordList =
- * ["hot","dot","dog","lot","log"]
- * Output: 0
- * Explanation: The endWord "cog" is not in wordList,
- * therefore there is no valid transformation sequence.
- */
+ /**
+ * Test 2:
+ * Input: beginWord = "hit", endWord = "cog", wordList =
+ * ["hot","dot","dog","lot","log"]
+ * Output: 0
+ * Explanation: The endWord "cog" is not in wordList,
+ * therefore there is no valid transformation sequence.
+ */
+ @Test
+ public void testWordLadder2() {
List wordList2 = Arrays.asList("hot", "dot", "dog", "lot", "log");
assertEquals(WordLadder.ladderLength("hit", "cog", wordList2), 0);
From bf4fc3f9c296c4510765f5bd8e885bed4c2f24dc Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Wed, 19 Jun 2024 07:18:53 +0200
Subject: [PATCH 026/656] Chore(deps): bump
org.apache.commons:commons-collections4 from 4.5.0-M1 to 4.5.0-M2 (#5240)
Chore(deps): bump org.apache.commons:commons-collections4
Bumps org.apache.commons:commons-collections4 from 4.5.0-M1 to 4.5.0-M2.
---
updated-dependencies:
- dependency-name: org.apache.commons:commons-collections4
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index a3a2b39e24de..6f28c62b5099 100644
--- a/pom.xml
+++ b/pom.xml
@@ -55,7 +55,7 @@
org.apache.commons
commons-collections4
- 4.5.0-M1
+ 4.5.0-M2
From a9db8428b29dc472c488191843af2110e115330c Mon Sep 17 00:00:00 2001
From: Alex K
Date: Wed, 19 Jun 2024 19:57:54 +0300
Subject: [PATCH 027/656] Refactoring BinaryInsertionSort according to common
SortAlgorithm approach (#5239)
* Refactoring BinaryInsertionSort according to common SortAlgorithm approach
* Formatting has been fixed
* Refactoring tests for BinaryInsertionSort according to SortingAlgorithmTest
* Removing redundant tests and improving variable readability
---------
Co-authored-by: alx
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
---
.../sorts/BinaryInsertionSort.java | 21 +++++++++++----
.../sorts/BinaryInsertionSortTest.java | 27 ++++---------------
2 files changed, 21 insertions(+), 27 deletions(-)
diff --git a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java
index 13076c617c76..b6f5d92e7928 100644
--- a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java
+++ b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java
@@ -1,17 +1,28 @@
package com.thealgorithms.sorts;
-public class BinaryInsertionSort {
+/**
+ * BinaryInsertionSort class implements the SortAlgorithm interface using the binary insertion sort technique.
+ * Binary Insertion Sort improves upon the simple insertion sort by using binary search to find the appropriate
+ * location to insert the new element, reducing the number of comparisons in the insertion step.
+ */
+public class BinaryInsertionSort implements SortAlgorithm {
- // Binary Insertion Sort method
- public int[] binaryInsertSort(int[] array) {
+ /**
+ * Sorts the given array using the Binary Insertion Sort algorithm.
+ *
+ * @param the type of elements in the array, which must implement the Comparable interface
+ * @param array the array to be sorted
+ * @return the sorted array
+ */
+ public > T[] sort(T[] array) {
for (int i = 1; i < array.length; i++) {
- int temp = array[i];
+ final T temp = array[i];
int low = 0;
int high = i - 1;
while (low <= high) {
final int mid = (low + high) >>> 1;
- if (temp < array[mid]) {
+ if (temp.compareTo(array[mid]) < 0) {
high = mid - 1;
} else {
low = mid + 1;
diff --git a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java
index bdd0702942a2..82cb3ba60987 100644
--- a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java
@@ -1,27 +1,10 @@
package com.thealgorithms.sorts;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+class BinaryInsertionSortTest extends SortingAlgorithmTest {
+ private final BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort();
-import org.junit.jupiter.api.Test;
-
-class BinaryInsertionSortTest {
-
- BinaryInsertionSort bis = new BinaryInsertionSort();
-
- @Test
- // valid test case
- public void binaryInsertionSortTestNonDuplicate() {
- int[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7};
- int[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- int[] actResult = bis.binaryInsertSort(array);
- assertArrayEquals(expResult, actResult);
- }
-
- @Test
- public void binaryInsertionSortTestDuplicate() {
- int[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6};
- int[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9};
- int[] actResult = bis.binaryInsertSort(array);
- assertArrayEquals(expResult, actResult);
+ @Override
+ SortAlgorithm getSortAlgorithm() {
+ return binaryInsertionSort;
}
}
From 91101ec424d5ee78c5e132e29d6fb7492601c2ef Mon Sep 17 00:00:00 2001
From: Alex K
Date: Thu, 20 Jun 2024 09:26:09 +0300
Subject: [PATCH 028/656] Refactoring and code improving for OddEvenSort
(#5242)
* Refactoring and code improving for OddEvenSort, changing it according to SortAlgorithm approach, also applying SortUtils
* Fix checkstyle
* Remove redundant main, remove redundant tests.
---------
Co-authored-by: alx
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
---
.../com/thealgorithms/sorts/OddEvenSort.java | 66 ++++++-------------
.../thealgorithms/sorts/OddEvenSortTest.java | 31 ++-------
2 files changed, 24 insertions(+), 73 deletions(-)
diff --git a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
index fb6e4d2649cb..8db85f39e9ac 100644
--- a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
+++ b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
@@ -1,69 +1,41 @@
package com.thealgorithms.sorts;
-import java.util.Random;
-
-// https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
-public final class OddEvenSort {
- private OddEvenSort() {
- }
-
- public static void main(String[] args) {
- int[] arr = new int[100];
-
- Random random = new Random();
-
- // Print out unsorted elements
- for (int i = 0; i < arr.length; ++i) {
- arr[i] = random.nextInt(100) - 50;
- System.out.println(arr[i]);
- }
- System.out.println("--------------");
-
- oddEvenSort(arr);
-
- // Print Sorted elements
- for (int i = 0; i < arr.length - 1; ++i) {
- System.out.println(arr[i]);
- assert arr[i] <= arr[i + 1];
- }
- }
+/**
+ * OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
+ * Odd-even sort is a comparison sort related to bubble sort.
+ * It operates by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order, swapping them.
+ * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
+ *
+ */
+public final class OddEvenSort implements SortAlgorithm {
/**
- * Odd Even Sort algorithms implements
+ * Sorts the given array using the Odd-Even Sort algorithm.
*
- * @param arr the array contains elements
+ * @param the type of elements in the array, which must implement the Comparable interface
+ * @param arr the array to be sorted
+ * @return the sorted array
*/
- public static void oddEvenSort(int[] arr) {
+ @Override
+ public > T[] sort(T[] arr) {
boolean sorted = false;
while (!sorted) {
sorted = true;
for (int i = 1; i < arr.length - 1; i += 2) {
- if (arr[i] > arr[i + 1]) {
- swap(arr, i, i + 1);
+ if (arr[i].compareTo(arr[i + 1]) > 0) {
+ SortUtils.swap(arr, i, i + 1);
sorted = false;
}
}
for (int i = 0; i < arr.length - 1; i += 2) {
- if (arr[i] > arr[i + 1]) {
- swap(arr, i, i + 1);
+ if (arr[i].compareTo(arr[i + 1]) > 0) {
+ SortUtils.swap(arr, i, i + 1);
sorted = false;
}
}
}
- }
-
- /**
- * Helper function to swap two array values.
- *
- * @param arr the array contains elements
- * @param i the first index to be swapped
- * @param j the second index to be swapped
- */
- private static void swap(int[] arr, int i, int j) {
- int temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
+ return arr;
}
}
diff --git a/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java b/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
index a7d0a58e229d..09ef8014cec1 100644
--- a/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
@@ -1,36 +1,15 @@
package com.thealgorithms.sorts;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-
-import org.junit.jupiter.api.Test;
-
/**
* @author Tabbygray (https://github.com/Tabbygray)
* @see OddEvenSort
*/
-public class OddEvenSortTest {
- @Test
- public void oddEvenSortEmptyArray() {
- int[] inputArray = {};
- OddEvenSort.oddEvenSort(inputArray);
- int[] expectedOutput = {};
- assertArrayEquals(inputArray, expectedOutput);
- }
-
- @Test
- public void oddEvenSortNaturalNumberArray() {
- int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
- OddEvenSort.oddEvenSort(inputArray);
- int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
- assertArrayEquals(inputArray, expectedOutput);
- }
+public class OddEvenSortTest extends SortingAlgorithmTest {
+ private final OddEvenSort oddEvenSort = new OddEvenSort();
- @Test
- public void oddEvenSortIntegerArray() {
- int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
- OddEvenSort.oddEvenSort(inputArray);
- int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
- assertArrayEquals(inputArray, expectedOutput);
+ @Override
+ SortAlgorithm getSortAlgorithm() {
+ return oddEvenSort;
}
}
From 15d2e706739451e0ca000bd63df82411d4d25053 Mon Sep 17 00:00:00 2001
From: Alex K
Date: Thu, 20 Jun 2024 18:47:43 +0300
Subject: [PATCH 029/656] Refactoring and code improving for StrandSort (#5243)
* Refactoring and code improving for StrandSort
* Fix java checkstyle
* Fix "Each variable declaration must be in its own statement"
* Fix "uses integer based for loops to iterate over a List"
---------
Co-authored-by: alx
---
.../com/thealgorithms/sorts/StrandSort.java | 83 +++++++++++++------
.../thealgorithms/sorts/StrandSortTest.java | 34 +-------
2 files changed, 62 insertions(+), 55 deletions(-)
diff --git a/src/main/java/com/thealgorithms/sorts/StrandSort.java b/src/main/java/com/thealgorithms/sorts/StrandSort.java
index 51600812bbb1..58cd35628506 100644
--- a/src/main/java/com/thealgorithms/sorts/StrandSort.java
+++ b/src/main/java/com/thealgorithms/sorts/StrandSort.java
@@ -1,46 +1,79 @@
package com.thealgorithms.sorts;
-import java.util.Iterator;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
-public final class StrandSort {
- private StrandSort() {
+/**
+ * StrandSort class implementing the SortAlgorithm interface using arrays.
+ */
+public final class StrandSort implements SortAlgorithm {
+
+ /**
+ * Sorts the given array using the Strand Sort algorithm.
+ *
+ * @param The type of elements to be sorted, must be Comparable.
+ * @param unsorted The array to be sorted.
+ * @return The sorted array.
+ */
+ @Override
+ public > T[] sort(T[] unsorted) {
+ List unsortedList = new ArrayList<>(Arrays.asList(unsorted));
+ List sortedList = strandSort(unsortedList);
+ return sortedList.toArray(unsorted);
}
- // note: the input list is destroyed
- public static > LinkedList strandSort(LinkedList list) {
+ /**
+ * Strand Sort algorithm that sorts a list.
+ *
+ * @param The type of elements to be sorted, must be Comparable.
+ * @param list The list to be sorted.
+ * @return The sorted list.
+ */
+ private static > List strandSort(List list) {
if (list.size() <= 1) {
return list;
}
- LinkedList result = new LinkedList();
- while (list.size() > 0) {
- LinkedList sorted = new LinkedList();
- sorted.add(list.removeFirst()); // same as remove() or remove(0)
- for (Iterator it = list.iterator(); it.hasNext();) {
- E elem = it.next();
- if (sorted.peekLast().compareTo(elem) <= 0) {
- sorted.addLast(elem); // same as add(elem) or add(0, elem)
- it.remove();
+ List result = new ArrayList<>();
+ while (!list.isEmpty()) {
+ final List sorted = new ArrayList<>();
+ sorted.add(list.remove(0));
+ for (int i = 0; i < list.size();) {
+ if (sorted.get(sorted.size() - 1).compareTo(list.get(i)) <= 0) {
+ sorted.add(list.remove(i));
+ } else {
+ i++;
}
}
- result = merge(sorted, result);
+ result = merge(result, sorted);
}
return result;
}
- private static > LinkedList merge(LinkedList left, LinkedList right) {
- LinkedList result = new LinkedList();
- while (!left.isEmpty() && !right.isEmpty()) {
- // change the direction of this comparison to change the direction of the sort
- if (left.peek().compareTo(right.peek()) <= 0) {
- result.add(left.remove());
+ /**
+ * Merges two sorted lists into one sorted list.
+ *
+ * @param The type of elements to be sorted, must be Comparable.
+ * @param left The first sorted list.
+ * @param right The second sorted list.
+ * @return The merged sorted list.
+ */
+ private static > List merge(List left, List right) {
+ List result = new ArrayList<>();
+ int i = 0;
+ int j = 0;
+ while (i < left.size() && j < right.size()) {
+ if (left.get(i).compareTo(right.get(j)) <= 0) {
+ result.add(left.get(i));
+ i++;
} else {
- result.add(right.remove());
+ result.add(right.get(j));
+ j++;
}
}
- result.addAll(left);
- result.addAll(right);
+ result.addAll(left.subList(i, left.size()));
+ result.addAll(right.subList(j, right.size()));
return result;
}
}
diff --git a/src/test/java/com/thealgorithms/sorts/StrandSortTest.java b/src/test/java/com/thealgorithms/sorts/StrandSortTest.java
index 91e85c81e5ec..a7250acf9bec 100644
--- a/src/test/java/com/thealgorithms/sorts/StrandSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/StrandSortTest.java
@@ -1,34 +1,8 @@
package com.thealgorithms.sorts;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-
-import java.util.Arrays;
-import java.util.LinkedList;
-import org.junit.jupiter.api.Test;
-
-class StrandSortTest {
-
- @Test
- // valid test case
- public void strandSortNonDuplicateTest() {
- int[] expectedArray = {1, 2, 3, 4, 5};
- LinkedList actualList = StrandSort.strandSort(new LinkedList(Arrays.asList(3, 1, 2, 4, 5)));
- int[] actualArray = new int[actualList.size()];
- for (int i = 0; i < actualList.size(); i++) {
- actualArray[i] = actualList.get(i);
- }
- assertArrayEquals(expectedArray, actualArray);
- }
-
- @Test
- // valid test case
- public void strandSortDuplicateTest() {
- int[] expectedArray = {2, 2, 2, 5, 7};
- LinkedList actualList = StrandSort.strandSort(new LinkedList(Arrays.asList(7, 2, 2, 2, 5)));
- int[] actualArray = new int[actualList.size()];
- for (int i = 0; i < actualList.size(); i++) {
- actualArray[i] = actualList.get(i);
- }
- assertArrayEquals(expectedArray, actualArray);
+class StrandSortTest extends SortingAlgorithmTest {
+ @Override
+ SortAlgorithm getSortAlgorithm() {
+ return new StrandSort();
}
}
From 8ef69bc85404a714c186f8aead26cd3d92595610 Mon Sep 17 00:00:00 2001
From: Alex Klymenko
Date: Fri, 21 Jun 2024 22:37:58 +0300
Subject: [PATCH 030/656] Improving BitonicSort (#5244)
* Improving BitonicSort
* Moving max method to SortingUtils
* Adding Javadoc to merge method
* Fix for test and code improvements
* Improving code readability
* Renaming method parameters
---------
Co-authored-by: alx
Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com>
---
.../com/thealgorithms/sorts/BitonicSort.java | 149 +++++++++++-------
.../thealgorithms/sorts/BitonicSortTest.java | 8 +
2 files changed, 99 insertions(+), 58 deletions(-)
create mode 100644 src/test/java/com/thealgorithms/sorts/BitonicSortTest.java
diff --git a/src/main/java/com/thealgorithms/sorts/BitonicSort.java b/src/main/java/com/thealgorithms/sorts/BitonicSort.java
index b4b26299562f..90d204818729 100644
--- a/src/main/java/com/thealgorithms/sorts/BitonicSort.java
+++ b/src/main/java/com/thealgorithms/sorts/BitonicSort.java
@@ -1,79 +1,112 @@
package com.thealgorithms.sorts;
-/* Java program for Bitonic Sort. Note that this program
-works only when size of input is a power of 2. */
-public class BitonicSort {
-
- /* The parameter dir indicates the sorting direction,
- ASCENDING or DESCENDING; if (a[i] > a[j]) agrees
- with the direction, then a[i] and a[j] are
- interchanged. */
- void compAndSwap(int[] a, int i, int j, int dir) {
- if ((a[i] > a[j] && dir == 1) || (a[i] < a[j] && dir == 0)) {
- // Swapping elements
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
+import java.util.Arrays;
+import java.util.function.BiPredicate;
+
+/**
+ * BitonicSort class implements the SortAlgorithm interface using the bitonic sort technique.
+ */
+public class BitonicSort implements SortAlgorithm {
+ private enum Direction {
+ DESCENDING,
+ ASCENDING,
}
- /* It recursively sorts a bitonic sequence in ascending
- order, if dir = 1, and in descending order otherwise
- (means dir=0). The sequence to be sorted starts at
- index position low, the parameter cnt is the number
- of elements to be sorted.*/
- void bitonicMerge(int[] a, int low, int cnt, int dir) {
- if (cnt > 1) {
- int k = cnt / 2;
- for (int i = low; i < low + k; i++) {
- compAndSwap(a, i, i + k, dir);
- }
- bitonicMerge(a, low, k, dir);
- bitonicMerge(a, low + k, k, dir);
+ /**
+ * Sorts the given array using the Bitonic Sort algorithm.
+ *
+ * @param the type of elements in the array, which must implement the Comparable interface
+ * @param array the array to be sorted
+ * @return the sorted array
+ */
+ @Override
+ public > T[] sort(T[] array) {
+ if (array.length == 0) {
+ return array;
}
+
+ final int paddedSize = nextPowerOfTwo(array.length);
+ T[] paddedArray = Arrays.copyOf(array, paddedSize);
+
+ // Fill the padded part with a maximum value
+ final T maxValue = max(array);
+ Arrays.fill(paddedArray, array.length, paddedSize, maxValue);
+
+ bitonicSort(paddedArray, 0, paddedSize, Direction.ASCENDING);
+ return Arrays.copyOf(paddedArray, array.length);
}
- /* This funcion first produces a bitonic sequence by
- recursively sorting its two halves in opposite sorting
- orders, and then calls bitonicMerge to make them in
- the same order */
- void bitonicSort(int[] a, int low, int cnt, int dir) {
+ private > void bitonicSort(final T[] array, final int low, final int cnt, final Direction direction) {
if (cnt > 1) {
- int k = cnt / 2;
+ final int k = cnt / 2;
- // sort in ascending order since dir here is 1
- bitonicSort(a, low, k, 1);
+ // Sort first half in ascending order
+ bitonicSort(array, low, k, Direction.ASCENDING);
- // sort in descending order since dir here is 0
- bitonicSort(a, low + k, k, 0);
+ // Sort second half in descending order
+ bitonicSort(array, low + k, cnt - k, Direction.DESCENDING);
- // Will merge whole sequence in ascending order
- // since dir=1.
- bitonicMerge(a, low, cnt, dir);
+ // Merge the whole sequence in ascending order
+ bitonicMerge(array, low, cnt, direction);
}
}
- /*Caller of bitonicSort for sorting the entire array
- of length N in ASCENDING order */
- void sort(int[] a, int n, int up) {
- bitonicSort(a, 0, n, up);
+ /**
+ * Merges the bitonic sequence in the specified direction.
+ *
+ * @param the type of elements in the array, which must be Comparable
+ * @param array the array containing the bitonic sequence to be merged
+ * @param low the starting index of the sequence to be merged
+ * @param cnt the number of elements in the sequence to be merged
+ * @param direction the direction of sorting
+ */
+ private > void bitonicMerge(T[] array, int low, int cnt, Direction direction) {
+ if (cnt > 1) {
+ final int k = cnt / 2;
+
+ final BiPredicate areSorted = (direction == Direction.ASCENDING) ? (a, b) -> a.compareTo(b) < 0 : (a, b) -> a.compareTo(b) > 0;
+ for (int i = low; i < low + k; i++) {
+ if (!areSorted.test(array[i], array[i + k])) {
+ SortUtils.swap(array, i, i + k);
+ }
+ }
+
+ bitonicMerge(array, low, k, direction);
+ bitonicMerge(array, low + k, cnt - k, direction);
+ }
}
- /* A utility function to print array of size n */
- static void printArray(int[] arr) {
- int n = arr.length;
- for (int i = 0; i < n; ++i) {
- System.out.print(arr[i] + " ");
+ /**
+ * Finds the next power of two greater than or equal to the given number.
+ *
+ * @param n the number
+ * @return the next power of two
+ */
+ private static int nextPowerOfTwo(int n) {
+ int count = 0;
+
+ // First n in the below condition is for the case where n is 0
+ if ((n & (n - 1)) == 0) {
+ return n;
+ }
+
+ while (n != 0) {
+ n >>= 1;
+ count += 1;
}
- System.out.println();
+
+ return 1 << count;
}
- public static void main(String[] args) {
- int[] a = {3, 7, 4, 8, 6, 2, 1, 5};
- int up = 1;
- BitonicSort ob = new BitonicSort();
- ob.sort(a, a.length, up);
- System.out.println("\nSorted array");
- printArray(a);
+ /**
+ * Finds the maximum element in the given array.
+ *
+ * @param the type of elements in the array, which must implement the Comparable interface
+ * @param array the array to be searched
+ * @return the maximum element in the array
+ * @throws IllegalArgumentException if the array is null or empty
+ */
+ private static