Skip to content

Commit f7ab05c

Browse files
Merge branch 'master' into master
2 parents 00ac924 + 8886e09 commit f7ab05c

37 files changed

+356
-142
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@
304304
* [WildcardMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java)
305305
* [WineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java)
306306
* geometry
307+
* [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
307308
* [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
309+
* [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
308310
* greedyalgorithms
309311
* [ActivitySelection](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java)
310312
* [BinaryAddition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/greedyalgorithms/BinaryAddition.java)
@@ -896,6 +898,7 @@
896898
* [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java)
897899
* [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java)
898900
* geometry
901+
* [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
899902
* [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
900903
* greedyalgorithms
901904
* [ActivitySelectionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/greedyalgorithms/ActivitySelectionTest.java)
@@ -1020,6 +1023,7 @@
10201023
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
10211024
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10221025
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
1026+
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
10231027
* [TwoSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/TwoSumProblemTest.java)
10241028
* [WordBoggleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/WordBoggleTest.java)
10251029
* others

spotbugs-exclude.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@
8787
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
8888
</Match>
8989
<!-- fb-contrib -->
90-
<Match>
91-
<Bug pattern="OCP_OVERLY_CONCRETE_PARAMETER" />
92-
</Match>
9390
<Match>
9491
<Bug pattern="LSC_LITERAL_STRING_COMPARISON" />
9592
</Match>

src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.backtracking;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -95,7 +96,7 @@ public static void removeWord(char[][] puzzle, String word, int row, int col, bo
9596
* @param words The list of words to be placed.
9697
* @return true if the crossword is solved, false otherwise.
9798
*/
98-
public static boolean solveCrossword(char[][] puzzle, List<String> words) {
99+
public static boolean solveCrossword(char[][] puzzle, Collection<String> words) {
99100
// Create a mutable copy of the words list
100101
List<String> remainingWords = new ArrayList<>(words);
101102

src/main/java/com/thealgorithms/datastructures/graphs/AStar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public int getEstimated() {
9494
}
9595

9696
// Initializes the graph with edges defined in the input data
97-
static void initializeGraph(Graph graph, ArrayList<Integer> data) {
97+
static void initializeGraph(Graph graph, List<Integer> data) {
9898
for (int i = 0; i < data.size(); i += 4) {
9999
graph.addEdge(new Edge(data.get(i), data.get(i + 1), data.get(i + 2)));
100100
}

src/main/java/com/thealgorithms/datastructures/graphs/EdmondsBlossomAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private EdmondsBlossomAlgorithm() {
3030
* @param vertexCount The number of vertices in the graph.
3131
* @return A list of matched pairs of vertices.
3232
*/
33-
public static List<int[]> maximumMatching(List<int[]> edges, int vertexCount) {
33+
public static List<int[]> maximumMatching(Iterable<int[]> edges, int vertexCount) {
3434
List<List<Integer>> graph = new ArrayList<>(vertexCount);
3535

3636
// Initialize each vertex's adjacency list.

src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.lists;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45
import java.util.List;
56

67
/**
@@ -38,7 +39,7 @@ public static void main(String[] args) {
3839
* @param listB the second list to merge
3940
* @param listC the result list after merging
4041
*/
41-
public static void merge(List<Integer> listA, List<Integer> listB, List<Integer> listC) {
42+
public static void merge(List<Integer> listA, List<Integer> listB, Collection<Integer> listC) {
4243
int pa = 0;
4344
/* the index of listA */
4445
int pb = 0;
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.Comparator;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
import java.util.TreeSet;
11+
12+
/**
13+
* A class providing algorithms to compute the convex hull of a set of points
14+
* using brute-force and recursive methods.
15+
*
16+
* Convex hull: The smallest convex polygon that contains all the given points.
17+
*
18+
* Algorithms provided:
19+
* 1. Brute-Force Method
20+
* 2. Recursive (Divide-and-Conquer) Method
21+
*
22+
* @author Hardvan
23+
*/
24+
public final class ConvexHull {
25+
private ConvexHull() {
26+
}
27+
28+
private static boolean checkPointOrientation(Point i, Point j, Point k) {
29+
int detK = Point.orientation(i, j, k);
30+
if (detK > 0) {
31+
return true; // pointsLeftOfIJ
32+
} else if (detK < 0) {
33+
return false; // pointsRightOfIJ
34+
} else {
35+
return k.compareTo(i) >= 0 && k.compareTo(j) <= 0;
36+
}
37+
}
38+
39+
public static List<Point> convexHullBruteForce(List<Point> points) {
40+
Set<Point> convexSet = new TreeSet<>(Comparator.naturalOrder());
41+
42+
for (int i = 0; i < points.size() - 1; i++) {
43+
for (int j = i + 1; j < points.size(); j++) {
44+
boolean allPointsOnOneSide = true;
45+
boolean leftSide = checkPointOrientation(points.get(i), points.get(j), points.get((i + 1) % points.size()));
46+
47+
for (int k = 0; k < points.size(); k++) {
48+
if (k != i && k != j && checkPointOrientation(points.get(i), points.get(j), points.get(k)) != leftSide) {
49+
allPointsOnOneSide = false;
50+
break;
51+
}
52+
}
53+
54+
if (allPointsOnOneSide) {
55+
convexSet.add(points.get(i));
56+
convexSet.add(points.get(j));
57+
}
58+
}
59+
}
60+
61+
return new ArrayList<>(convexSet);
62+
}
63+
64+
public static List<Point> convexHullRecursive(List<Point> points) {
65+
Collections.sort(points);
66+
Set<Point> convexSet = new HashSet<>();
67+
Point leftMostPoint = points.get(0);
68+
Point rightMostPoint = points.get(points.size() - 1);
69+
70+
convexSet.add(leftMostPoint);
71+
convexSet.add(rightMostPoint);
72+
73+
List<Point> upperHull = new ArrayList<>();
74+
List<Point> lowerHull = new ArrayList<>();
75+
76+
for (int i = 1; i < points.size() - 1; i++) {
77+
int det = Point.orientation(leftMostPoint, rightMostPoint, points.get(i));
78+
if (det > 0) {
79+
upperHull.add(points.get(i));
80+
} else if (det < 0) {
81+
lowerHull.add(points.get(i));
82+
}
83+
}
84+
85+
constructHull(upperHull, leftMostPoint, rightMostPoint, convexSet);
86+
constructHull(lowerHull, rightMostPoint, leftMostPoint, convexSet);
87+
88+
List<Point> result = new ArrayList<>(convexSet);
89+
Collections.sort(result);
90+
return result;
91+
}
92+
93+
private static void constructHull(Collection<Point> points, Point left, Point right, Set<Point> convexSet) {
94+
if (!points.isEmpty()) {
95+
Point extremePoint = null;
96+
int extremePointDistance = Integer.MIN_VALUE;
97+
List<Point> candidatePoints = new ArrayList<>();
98+
99+
for (Point p : points) {
100+
int det = Point.orientation(left, right, p);
101+
if (det > 0) {
102+
candidatePoints.add(p);
103+
if (det > extremePointDistance) {
104+
extremePointDistance = det;
105+
extremePoint = p;
106+
}
107+
}
108+
}
109+
110+
if (extremePoint != null) {
111+
constructHull(candidatePoints, left, extremePoint, convexSet);
112+
convexSet.add(extremePoint);
113+
constructHull(candidatePoints, extremePoint, right, convexSet);
114+
}
115+
}
116+
}
117+
}

src/main/java/com/thealgorithms/geometry/GrahamScan.java

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5-
import java.util.Comparator;
65
import java.util.Stack;
76

87
/**
@@ -66,93 +65,4 @@ public GrahamScan(Point[] points) {
6665
public Iterable<Point> hull() {
6766
return new ArrayList<>(hull);
6867
}
69-
70-
public record Point(int x, int y) implements Comparable<Point> {
71-
72-
/**
73-
* Default constructor
74-
* @param x x-coordinate
75-
* @param y y-coordinate
76-
*/
77-
public Point {
78-
}
79-
80-
/**
81-
* @return the x-coordinate
82-
*/
83-
@Override
84-
public int x() {
85-
return x;
86-
}
87-
88-
/**
89-
* @return the y-coordinate
90-
*/
91-
@Override
92-
public int y() {
93-
return y;
94-
}
95-
96-
/**
97-
* Determines the orientation of the triplet (a, b, c).
98-
*
99-
* @param a The first point
100-
* @param b The second point
101-
* @param c The third point
102-
* @return -1 if (a, b, c) is clockwise, 0 if collinear, +1 if counterclockwise
103-
*/
104-
public static int orientation(Point a, Point b, Point c) {
105-
int val = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
106-
return Integer.compare(val, 0);
107-
}
108-
109-
/**
110-
* Compares this point with another point.
111-
*
112-
* @param p2 The point to compare to
113-
* @return A positive integer if this point is greater, a negative integer if less, or 0 if equal
114-
*/
115-
@Override
116-
public int compareTo(Point p2) {
117-
int cmpY = Integer.compare(this.y, p2.y);
118-
return cmpY != 0 ? cmpY : Integer.compare(this.x, p2.x);
119-
}
120-
121-
/**
122-
* Returns a comparator to sort points by their polar order relative to this point.
123-
*
124-
* @return A polar order comparator
125-
*/
126-
public Comparator<Point> polarOrder() {
127-
return new PolarOrder();
128-
}
129-
130-
private final class PolarOrder implements Comparator<Point> {
131-
@Override
132-
public int compare(Point p1, Point p2) {
133-
int dx1 = p1.x - x;
134-
int dy1 = p1.y - y;
135-
int dx2 = p2.x - x;
136-
int dy2 = p2.y - y;
137-
138-
if (dy1 >= 0 && dy2 < 0) {
139-
return -1; // p1 above p2
140-
} else if (dy2 >= 0 && dy1 < 0) {
141-
return 1; // p1 below p2
142-
} else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal
143-
return Integer.compare(dx2, dx1);
144-
} else {
145-
return -orientation(Point.this, p1, p2); // Compare orientation
146-
}
147-
}
148-
}
149-
150-
/**
151-
* @return A string representation of this point in the format (x, y)
152-
*/
153-
@Override
154-
public String toString() {
155-
return String.format("(%d, %d)", x, y);
156-
}
157-
}
15868
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.util.Comparator;
4+
5+
public record Point(int x, int y) implements Comparable<Point> {
6+
7+
@Override
8+
public int compareTo(Point other) {
9+
int cmpY = Integer.compare(this.y, other.y);
10+
return cmpY != 0 ? cmpY : Integer.compare(this.x, other.x);
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return String.format("(%d, %d)", x, y);
16+
}
17+
18+
public Comparator<Point> polarOrder() {
19+
return new PolarOrder();
20+
}
21+
22+
public static int orientation(Point a, Point b, Point c) {
23+
return Integer.compare((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x), 0);
24+
}
25+
26+
private final class PolarOrder implements Comparator<Point> {
27+
@Override
28+
public int compare(Point p1, Point p2) {
29+
int dx1 = p1.x - x;
30+
int dy1 = p1.y - y;
31+
int dx2 = p2.x - x;
32+
int dy2 = p2.y - y;
33+
34+
if (dy1 >= 0 && dy2 < 0) {
35+
return -1; // p1 above p2
36+
} else if (dy2 >= 0 && dy1 < 0) {
37+
return 1; // p1 below p2
38+
} else if (dy1 == 0 && dy2 == 0) { // Collinear and horizontal
39+
return Integer.compare(dx2, dx1);
40+
} else {
41+
return -orientation(Point.this, p1, p2); // Compare orientation
42+
}
43+
}
44+
}
45+
}

src/main/java/com/thealgorithms/maths/CircularConvolutionFFT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45

56
/**
67
* Class for circular convolution of two discrete signals using the convolution
@@ -19,7 +20,7 @@ private CircularConvolutionFFT() {
1920
* @param x The signal to be padded.
2021
* @param newSize The new size of the signal.
2122
*/
22-
private static void padding(ArrayList<FFT.Complex> x, int newSize) {
23+
private static void padding(Collection<FFT.Complex> x, int newSize) {
2324
if (x.size() < newSize) {
2425
int diff = newSize - x.size();
2526
for (int i = 0; i < diff; i++) {

src/main/java/com/thealgorithms/maths/ConvolutionFFT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.maths;
22

33
import java.util.ArrayList;
4+
import java.util.Collection;
45

56
/**
67
* Class for linear convolution of two discrete signals using the convolution
@@ -19,7 +20,7 @@ private ConvolutionFFT() {
1920
* @param x The signal to be padded.
2021
* @param newSize The new size of the signal.
2122
*/
22-
private static void padding(ArrayList<FFT.Complex> x, int newSize) {
23+
private static void padding(Collection<FFT.Complex> x, int newSize) {
2324
if (x.size() < newSize) {
2425
int diff = newSize - x.size();
2526
for (int i = 0; i < diff; i++) {

0 commit comments

Comments
 (0)