Skip to content

Commit e6df7dd

Browse files
authored
Merge branch 'master' into master
2 parents a506f89 + a8bc9cc commit e6df7dd

File tree

15 files changed

+850
-22
lines changed

15 files changed

+850
-22
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>org.mockito</groupId>
4444
<artifactId>mockito-core</artifactId>
45-
<version>5.20.0</version>
45+
<version>5.21.0</version>
4646
<scope>test</scope>
4747
</dependency>
4848
<dependency>
@@ -127,7 +127,7 @@
127127
<plugin>
128128
<groupId>com.mebigfatguy.fb-contrib</groupId>
129129
<artifactId>fb-contrib</artifactId>
130-
<version>7.7.1</version>
130+
<version>7.7.2</version>
131131
</plugin>
132132
<plugin>
133133
<groupId>com.h3xstream.findsecbugs</groupId>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.physics;
2+
3+
/**
4+
* Calculates refraction angle using Snell's Law:
5+
* n1 * sin(theta1) = n2 * sin(theta2)
6+
* @see <a href="https://en.wikipedia.org/wiki/Snell%27s_law">Snell's Law</a>
7+
*/
8+
public final class SnellLaw {
9+
10+
private SnellLaw() {
11+
throw new AssertionError("No instances.");
12+
}
13+
14+
/**
15+
* Computes the refracted angle (theta2) in radians.
16+
*
17+
* @param n1 index of refraction of medium 1
18+
* @param n2 index of refraction of medium 2
19+
* @param theta1 incident angle in radians
20+
* @return refracted angle (theta2) in radians
21+
* @throws IllegalArgumentException if total internal reflection occurs
22+
*/
23+
public static double refractedAngle(double n1, double n2, double theta1) {
24+
double ratio = n1 / n2;
25+
double sinTheta2 = ratio * Math.sin(theta1);
26+
27+
if (Math.abs(sinTheta2) > 1.0) {
28+
throw new IllegalArgumentException("Total internal reflection: no refraction possible.");
29+
}
30+
31+
return Math.asin(sinTheta2);
32+
}
33+
}

src/main/java/com/thealgorithms/sorts/BubbleSort.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class BubbleSort implements SortAlgorithm {
1010
/**
1111
* Implements generic bubble sort algorithm.
1212
*
13+
* Time Complexity:
14+
* - Best case: O(n) – array is already sorted.
15+
* - Average case: O(n^2)
16+
* - Worst case: O(n^2)
17+
*
18+
* Space Complexity: O(1) – in-place sorting.
19+
*
1320
* @param array the array to be sorted.
1421
* @param <T> the type of elements in the array.
1522
* @return the sorted array.

src/main/java/com/thealgorithms/sorts/HeapSort.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package com.thealgorithms.sorts;
22

33
/**
4-
* Heap Sort Algorithm Implementation
4+
* Heap Sort algorithm implementation.
5+
*
6+
* Heap sort converts the array into a max-heap and repeatedly extracts the maximum
7+
* element to sort the array in increasing order.
8+
*
9+
* Time Complexity:
10+
* - Best case: O(n log n)
11+
* - Average case: O(n log n)
12+
* - Worst case: O(n log n)
13+
*
14+
* Space Complexity: O(1) – in-place sorting
515
*
616
* @see <a href="https://en.wikipedia.org/wiki/Heapsort">Heap Sort Algorithm</a>
17+
* @see SortAlgorithm
718
*/
819
public class HeapSort implements SortAlgorithm {
920

src/main/java/com/thealgorithms/sorts/InsertionSort.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
package com.thealgorithms.sorts;
22

3+
/**
4+
* Generic Insertion Sort algorithm.
5+
*
6+
* Standard insertion sort iterates through the array and inserts each element into its
7+
* correct position in the sorted portion of the array.
8+
*
9+
* Sentinel sort is a variation that first places the minimum element at index 0 to
10+
* avoid redundant comparisons in subsequent passes.
11+
*
12+
* Time Complexity:
13+
* - Best case: O(n) – array is already sorted (sentinel sort can improve slightly)
14+
* - Average case: O(n^2)
15+
* - Worst case: O(n^2) – array is reverse sorted
16+
*
17+
* Space Complexity: O(1) – in-place sorting
18+
*
19+
* @see SortAlgorithm
20+
*/
321
class InsertionSort implements SortAlgorithm {
422

523
/**

src/main/java/com/thealgorithms/sorts/MergeSort.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ class MergeSort implements SortAlgorithm {
1313
private Comparable[] aux;
1414

1515
/**
16-
* Generic merge sort algorithm implements.
16+
* Generic merge sort algorithm.
1717
*
18-
* @param unsorted the array which should be sorted.
19-
* @param <T> Comparable class.
20-
* @return sorted array.
18+
* Time Complexity:
19+
* - Best case: O(n log n)
20+
* - Average case: O(n log n)
21+
* - Worst case: O(n log n)
22+
*
23+
* Space Complexity: O(n) – requires auxiliary array for merging.
24+
*
25+
* @see SortAlgorithm
2126
*/
2227
@Override
2328
public <T extends Comparable<T>> T[] sort(T[] unsorted) {

src/main/java/com/thealgorithms/sorts/QuickSort.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88
class QuickSort implements SortAlgorithm {
99

1010
/**
11-
* This method implements the Generic Quick Sort
11+
* Generic Quick Sort algorithm.
1212
*
13-
* @param array The array to be sorted Sorts the array in increasing order
13+
* Time Complexity:
14+
* - Best case: O(n log n) – pivot splits array roughly in half each time.
15+
* - Average case: O(n log n)
16+
* - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
17+
*
18+
* Space Complexity: O(log n) – recursion stack, in-place sorting.
19+
*
20+
* @see SortAlgorithm
1421
*/
1522
@Override
1623
public <T extends Comparable<T>> T[] sort(T[] array) {

src/main/java/com/thealgorithms/sorts/SelectionSort.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
public class SelectionSort implements SortAlgorithm {
44
/**
5-
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
5+
* Generic Selection Sort algorithm.
66
*
7-
* @param array the array to be sorted
8-
* @param <T> the class of array elements
9-
* @return the sorted array
7+
* Time Complexity:
8+
* - Best case: O(n^2)
9+
* - Average case: O(n^2)
10+
* - Worst case: O(n^2)
11+
*
12+
* Space Complexity: O(1) – in-place sorting.
13+
*
14+
* @see SortAlgorithm
1015
*/
1116
@Override
1217
public <T extends Comparable<T>> T[] sort(T[] array) {

src/main/java/com/thealgorithms/sorts/TopologicalSort.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
* a linked list. A Directed Graph is proven to be acyclic when a DFS or Depth First Search is
1212
* performed, yielding no back-edges.
1313
*
14-
* https://en.wikipedia.org/wiki/Topological_sorting
14+
* Time Complexity: O(V + E)
15+
* - V: number of vertices
16+
* - E: number of edges
1517
*
16-
* @author Jonathan Taylor (https://github.com/Jtmonument)
18+
* Space Complexity: O(V + E)
19+
* - adjacency list and recursion stack in DFS
20+
*
21+
* Reference: https://en.wikipedia.org/wiki/Topological_sorting
22+
*
23+
* Author: Jonathan Taylor (https://github.com/Jtmonument)
1724
* Based on Introduction to Algorithms 3rd Edition
1825
*/
1926
public final class TopologicalSort {

src/main/java/com/thealgorithms/strings/Upper.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,27 @@ public static void main(String[] args) {
1515
}
1616

1717
/**
18-
* Converts all the characters in this {@code String} to upper case
18+
* Converts all the characters in this {@code String} to upper case.
1919
*
2020
* @param s the string to convert
2121
* @return the {@code String}, converted to uppercase.
2222
*/
2323
public static String toUpperCase(String s) {
2424
if (s == null) {
25-
throw new IllegalArgumentException("Input string connot be null");
25+
throw new IllegalArgumentException("Input string cannot be null");
2626
}
2727
if (s.isEmpty()) {
2828
return s;
2929
}
30-
StringBuilder result = new StringBuilder(s);
31-
for (int i = 0; i < result.length(); ++i) {
32-
char currentChar = result.charAt(i);
33-
if (Character.isLetter(currentChar) && Character.isLowerCase(currentChar)) {
34-
result.setCharAt(i, Character.toUpperCase(currentChar));
30+
31+
StringBuilder result = new StringBuilder(s.length());
32+
33+
for (int i = 0; i < s.length(); ++i) {
34+
char currentChar = s.charAt(i);
35+
if (Character.isLowerCase(currentChar)) {
36+
result.append(Character.toUpperCase(currentChar));
37+
} else {
38+
result.append(currentChar);
3539
}
3640
}
3741
return result.toString();

0 commit comments

Comments
 (0)