Skip to content

Commit 07276fc

Browse files
resolved errors
1 parent e518ce8 commit 07276fc

File tree

8 files changed

+88
-66
lines changed

8 files changed

+88
-66
lines changed

src/main/java/com/thealgorithms/searches/BinarySearch.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ class BinarySearch implements SearchAlgorithm {
2525
*/
2626
@Override
2727
public <T extends Comparable<T>> int find(T[] array, T key) {
28+
if (array == null) {
29+
throw new NullPointerException("Input array cannot be null");
30+
}
31+
if (key == null) {
32+
throw new NullPointerException("Key cannot be null");
33+
}
34+
if (array.length == 0) {
35+
return -1; // Explicitly handle empty array
36+
}
2837
return search(array, key, 0, array.length - 1);
2938
}
3039

@@ -53,4 +62,4 @@ private <T extends Comparable<T>> int search(T[] array, T key, int left, int rig
5362
return search(array, key, median + 1, right);
5463
}
5564
}
56-
}
65+
}

src/main/java/com/thealgorithms/shufflealogrithm/UniquePairShuffle.java

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

33
import java.util.ArrayList;
44
import java.util.Collections;
5-
import java.util.HashSet;
65
import java.util.List;
7-
import java.util.Set;
86

97
public
108
final class UniquePairShuffle {

src/main/java/com/thealgorithms/shufflealogrithm/WeightedShuffle.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import java.util.Comparator;
55
import java.util.Random;
66

7-
public
8-
final class WeightedShuffle {
7+
public final class WeightedShuffle {
98

10-
private
11-
WeightedShuffle() {
9+
private WeightedShuffle() {
1210
// Prevent instantiation
1311
}
1412

@@ -19,10 +17,9 @@ final class WeightedShuffle {
1917
* @param array the input array to shuffle
2018
* @param weights the weights for each corresponding element in the array
2119
*/
22-
public
23-
static void weightedShuffle(int[] array, int[] weights) {
20+
public static void weightedShuffle(int[] array, int[] weights) {
2421
// Edge case: Check if weights match the array size
25-
if (array == null || weights == null || array.length != weights.length) {
22+
if (array == null || weights == null || array.length!= weights.length) {
2623
return;
2724
}
2825

@@ -34,8 +31,8 @@ static void weightedShuffle(int[] array, int[] weights) {
3431
Random random = new Random();
3532

3633
// Sort indices by weights in descending order, prioritizing higher weights
37-
Arrays.sort(indices, Comparator.comparingInt((Integer i)->- weights[i])
38-
.thenComparingInt(i->random.nextInt()));
34+
Arrays.sort(indices, Comparator.comparingInt((Integer i) -> -weights[i])
35+
.thenComparingInt(i -> random.nextInt()));
3936

4037
int[] result = new int[array.length];
4138
for (int i = 0; i < array.length; i++) {
@@ -45,8 +42,7 @@ static void weightedShuffle(int[] array, int[] weights) {
4542
System.arraycopy(result, 0, array, 0, array.length);
4643
}
4744

48-
public
49-
static void main(String[] args) {
45+
public static void main(String[] args) {
5046
int[] array = {10, 20, 30};
5147
int[] weights = {1, 3, 2};
5248
weightedShuffle(array, weights);
@@ -56,4 +52,4 @@ static void main(String[] args) {
5652
System.out.print(num + " ");
5753
}
5854
}
59-
}
55+
}

src/test/java/com/thealgorithms/searches/BinarySearchTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.thealgorithms.searches;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.Assertions;
45

56
import java.util.stream.IntStream;
6-
import org.junit.jupiter.api.Test;
77

88
/**
99
* Unit tests for the BinarySearch class.
@@ -19,7 +19,7 @@ void testBinarySearchFound() {
1919
Integer[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2020
int key = 7;
2121
int expectedIndex = 6; // Index of the key in the array
22-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6.");
22+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the found element should be 6.");
2323
}
2424

2525
/**
@@ -31,7 +31,7 @@ void testBinarySearchNotFound() {
3131
Integer[] array = {1, 2, 3, 4, 5};
3232
int key = 6; // Element not present in the array
3333
int expectedIndex = -1; // Key not found
34-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
34+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
3535
}
3636

3737
/**
@@ -43,7 +43,7 @@ void testBinarySearchFirstElement() {
4343
Integer[] array = {1, 2, 3, 4, 5};
4444
int key = 1; // First element
4545
int expectedIndex = 0; // Index of the key in the array
46-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0.");
46+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the first element should be 0.");
4747
}
4848

4949
/**
@@ -55,7 +55,7 @@ void testBinarySearchLastElement() {
5555
Integer[] array = {1, 2, 3, 4, 5};
5656
int key = 5; // Last element
5757
int expectedIndex = 4; // Index of the key in the array
58-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4.");
58+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 4.");
5959
}
6060

6161
/**
@@ -67,7 +67,7 @@ void testBinarySearchSingleElementFound() {
6767
Integer[] array = {1};
6868
int key = 1; // Only element present
6969
int expectedIndex = 0; // Index of the key in the array
70-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0.");
70+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the single element should be 0.");
7171
}
7272

7373
/**
@@ -79,7 +79,7 @@ void testBinarySearchSingleElementNotFound() {
7979
Integer[] array = {1};
8080
int key = 2; // Key not present
8181
int expectedIndex = -1; // Key not found
82-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
82+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in the array.");
8383
}
8484

8585
/**
@@ -91,7 +91,7 @@ void testBinarySearchEmptyArray() {
9191
Integer[] array = {}; // Empty array
9292
int key = 1; // Key not present
9393
int expectedIndex = -1; // Key not found
94-
assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array.");
94+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The element should not be found in an empty array.");
9595
}
9696

9797
/**
@@ -103,6 +103,6 @@ void testBinarySearchLargeArray() {
103103
Integer[] array = IntStream.range(0, 10000).boxed().toArray(Integer[] ::new); // Array from 0 to 9999
104104
int key = 9999; // Last element
105105
int expectedIndex = 9999; // Index of the last element
106-
assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
106+
Assertions.assertEquals(expectedIndex, binarySearch.find(array, key), "The index of the last element should be 9999.");
107107
}
108-
}
108+
}

src/test/java/com/thealgorithms/shufflealgorithm/GroupShuffleTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void testGroupShuffleBasic() {
1919
assertEquals(3, shuffledGroups.size()); // Expect 3 groups
2020
assertTrue(shuffledGroups.stream().allMatch(
2121
group -> group.size() <= 3)); // All groups should have size <= 3
22-
System.out.println("Shuffled Groups (Basic Test): " + shuffledGroups);
22+
System.out.println("Shuffled Groups (Basic Test): "
23+
+ shuffledGroups);
2324
}
2425

2526
// Test case to check when group size is larger than array length
@@ -32,8 +33,8 @@ void testGroupShuffleLargeGroupSize() {
3233
assertEquals(
3334
Arrays.asList(1, 2, 3),
3435
shuffledGroups.get(0)); // The group should contain all elements
35-
System.out.println("Shuffled Groups (Large Group Size Test): " +
36-
shuffledGroups);
36+
System.out.println("Shuffled Groups (Large Group Size Test): "
37+
+ shuffledGroups);
3738
}
3839

3940
// Test case to check when the array is null
@@ -42,7 +43,8 @@ void testGroupShuffleNullArray() {
4243
List<List<Integer>> shuffledGroups = GroupShuffle.groupShuffle(null, 3);
4344

4445
assertTrue(shuffledGroups.isEmpty()); // Expect empty list
45-
System.out.println("Shuffled Groups (Null Array Test): " + shuffledGroups);
46+
System.out.println("Shuffled Groups (Null Array Test): "
47+
+ shuffledGroups);
4648
}
4749

4850
// Test case to check when group size is less than or equal to zero
@@ -56,8 +58,8 @@ void testGroupShuffleZeroOrNegativeGroupSize() {
5658
shuffledGroups = GroupShuffle.groupShuffle(array, -1);
5759
assertTrue(
5860
shuffledGroups.isEmpty()); // Expect empty list for negative group size
59-
System.out.println("Shuffled Groups (Zero or Negative Group Size Test): " +
60-
shuffledGroups);
61+
System.out.println("Shuffled Groups (Zero or Negative Group Size Test): "
62+
+ shuffledGroups);
6163
}
6264

6365
// Test case to check when the array has fewer than 3 elements
@@ -70,7 +72,8 @@ void testGroupShuffleSmallArray() {
7072
assertEquals(
7173
Arrays.asList(1, 2),
7274
shuffledGroups.get(0)); // The group should contain all elements
73-
System.out.println("Shuffled Groups (Small Array Test): " + shuffledGroups);
75+
System.out.println("Shuffled Groups (Small Array Test): "
76+
+ shuffledGroups);
7477
}
7578

7679
// Test case to check the behavior when the group size is 1
@@ -85,7 +88,7 @@ void testGroupShuffleGroupSizeOne() {
8588
Arrays.asList(i + 1),
8689
shuffledGroups.get(i)); // Each group should contain one element
8790
}
88-
System.out.println("Shuffled Groups (Group Size One Test): " +
89-
shuffledGroups);
91+
System.out.println("Shuffled Groups (Group Size One Test): "
92+
+ shuffledGroups);
9093
}
91-
}
94+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.thealgorithms.shufflealgorithm;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.Assertions;
45

56
import com.thealgorithms.shufflealogrithm.ShuffleByRange;
6-
import org.junit.jupiter.api.Test;
77

88
public class ShuffleByRangeTest {
99

@@ -14,56 +14,56 @@ void testShuffleByRangeBasic() {
1414
ShuffleByRange.shuffleByRange(array, 1, 5);
1515

1616
// Verify that elements outside the specified range remain unchanged
17-
assertEquals(1, array[0], "First element should be unchanged");
18-
assertEquals(6, array[5], "Last element should be unchanged");
17+
Assertions.assertEquals(1, array[0], "First element should be unchanged");
18+
Assertions.assertEquals(6, array[5], "Last element should be unchanged");
1919
}
2020

2121
// Test case for an empty array
2222
@Test
2323
void testShuffleByRangeEmptyArray() {
2424
int[] array = {};
2525
ShuffleByRange.shuffleByRange(array, 0, 1);
26-
assertArrayEquals(new int[] {}, array);
26+
Assertions.assertArrayEquals(new int[] {}, array);
2727
}
2828

2929
// Test case for a single element array
3030
@Test
3131
void testShuffleByRangeSingleElementArray() {
3232
int[] array = {1};
3333
ShuffleByRange.shuffleByRange(array, 0, 1);
34-
assertArrayEquals(new int[] {1}, array);
34+
Assertions.assertArrayEquals(new int[] {1}, array);
3535
}
3636

3737
// Test case for invalid range: start index equal to end index
3838
@Test
3939
void testShuffleByRangeStartEqualsEnd() {
4040
int[] array = {1, 2, 3, 4, 5};
4141
ShuffleByRange.shuffleByRange(array, 2, 2);
42-
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
42+
Assertions.assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
4343
}
4444

4545
// Test case for invalid range: start index out of bounds
4646
@Test
4747
void testShuffleByRangeStartOutOfBounds() {
4848
int[] array = {1, 2, 3, 4, 5};
4949
ShuffleByRange.shuffleByRange(array, -1, 5);
50-
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
50+
Assertions.assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
5151
}
5252

5353
// Test case for invalid range: end index out of bounds
5454
@Test
5555
void testShuffleByRangeEndOutOfBounds() {
5656
int[] array = {1, 2, 3, 4, 5};
5757
ShuffleByRange.shuffleByRange(array, 1, 6);
58-
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
58+
Assertions.assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
5959
}
6060

6161
// Test case for invalid range: start index greater than end index
6262
@Test
6363
void testShuffleByRangeStartGreaterThanEnd() {
6464
int[] array = {1, 2, 3, 4, 5};
6565
ShuffleByRange.shuffleByRange(array, 3, 2);
66-
assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
66+
Assertions.assertArrayEquals(new int[] {1, 2, 3, 4, 5}, array);
6767
}
6868

6969
// Test case for shuffling a large array
@@ -77,12 +77,12 @@ void testShuffleByRangeLargeArray() {
7777

7878
// Verify that the first 250 and last 250 elements remain unchanged
7979
for (int i = 0; i < 250; i++) {
80-
assertEquals(i + 1, array[i],
80+
Assertions.assertEquals(i + 1, array[i],
8181
"Elements at index " + i + " should be unchanged");
8282
}
8383
for (int i = 750; i < 1000; i++) {
84-
assertEquals(i + 1, array[i],
84+
Assertions.assertEquals(i + 1, array[i],
8585
"Elements at index " + i + " should be unchanged");
8686
}
8787
}
88-
}
88+
}

src/test/java/com/thealgorithms/shufflealgorithm/UniquePairShuffleTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void testPairShuffleSingleElementArray() {
6161
assertTrue(pairs.isEmpty());
6262
}
6363

64-
// Test case for two elements
64+
// Test case for two elements, adjusted to not expect a specific order
6565
@Test
6666
void testPairShuffleTwoElements() {
6767
int[] array = {1, 2};
@@ -71,8 +71,15 @@ void testPairShuffleTwoElements() {
7171
assertEquals(1, pairs.size());
7272
int[] pair = pairs.get(0);
7373

74-
// Check that the pair contains both elements, regardless of order
74+
// Check that the pair contains both elements, without specifying order
7575
assertTrue((pair[0] == 1 && pair[1] == 2) || (pair[0] == 2 && pair[1] == 1));
76+
77+
// Alternatively, for clarity and to directly address the error,
78+
// you could assert the pair's elements without considering order:
79+
int min = Math.min(pair[0], pair[1]);
80+
int max = Math.max(pair[0], pair[1]);
81+
assertEquals(1, min, "First element of the pair should be 1 when ordered");
82+
assertEquals(2, max, "Second element of the pair should be 2 when ordered");
7683
}
7784

7885
// Test case for larger even-length array

0 commit comments

Comments
 (0)