Skip to content

Commit 545aeae

Browse files
resolved issues
1 parent 0160f11 commit 545aeae

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.util.Arrays;
44
import java.util.Random;
55

6-
public final class ConstrainedShuffle {
6+
public
7+
final class ConstrainedShuffle {
78

8-
private ConstrainedShuffle() {
9+
private
10+
ConstrainedShuffle() {
911
// Prevent instantiation
1012
}
1113

@@ -15,25 +17,29 @@ private ConstrainedShuffle() {
1517
*
1618
* @param array the input array to shuffle
1719
*/
18-
public static void constrainedShuffle(int[] array) {
20+
public
21+
static void constrainedShuffle(int[] array) {
1922
// Edge case: If the array has less than 3 elements, no shuffling can occur
2023
if (array == null || array.length < 3) {
2124
return;
2225
}
2326

2427
Random random = new Random();
25-
for (int i = array.length - 1; i > 1; i--) {
28+
// Start loop from the second last element and exclude the first and last
29+
// elements
30+
for (int i = array.length - 2; i > 1; i--) {
2631
// Generate a random index between 1 and i (inclusive)
2732
int j = random.nextInt(i - 1) + 1;
2833

2934
// Swap the elements at positions i and j
30-
int temp = array[i]; // Temporarily store the element at i
31-
array[i] = array[j]; // Move element from j to i
32-
array[j] = temp; // Place the stored element in position j
35+
int temp = array[i];
36+
array[i] = array[j];
37+
array[j] = temp;
3338
}
3439
}
3540

36-
public static void main(String[] args) {
41+
public
42+
static void main(String[] args) {
3743
int[] array = {1, 2, 3, 4, 5};
3844
System.out.println("Original Array: " + Arrays.toString(array));
3945
constrainedShuffle(array);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import java.util.Collections;
55
import java.util.List;
66

7-
public final class GroupShuffle {
7+
public
8+
final class GroupShuffle {
89

9-
private GroupShuffle() {
10+
private
11+
GroupShuffle() {
1012
// Prevent instantiation
1113
}
1214

@@ -17,7 +19,8 @@ private GroupShuffle() {
1719
* @param groupSize the size of each group
1820
* @return a list of shuffled groups
1921
*/
20-
public static List<List<Integer>> groupShuffle(int[] array, int groupSize) {
22+
public
23+
static List<List<Integer>> groupShuffle(int[] array, int groupSize) {
2124
List<List<Integer>> groups = new ArrayList<>();
2225

2326
// Edge case: Check if the group size is valid
@@ -33,12 +36,16 @@ public static List<List<Integer>> groupShuffle(int[] array, int groupSize) {
3336
groups.add(group);
3437
}
3538

36-
// Shuffle the groups to randomize their order
37-
Collections.shuffle(groups);
39+
// Shuffle only if the group size is greater than 1
40+
if (groupSize > 1) {
41+
Collections.shuffle(groups);
42+
}
43+
3844
return groups;
3945
}
4046

41-
public static void main(String[] args) {
47+
public
48+
static void main(String[] args) {
4249
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
4350
List<List<Integer>> shuffledGroups = groupShuffle(array, 3);
4451

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import java.util.Arrays;
44
import java.util.Comparator;
5+
import java.util.Random;
56

6-
public final class WeightedShuffle {
7+
public
8+
final class WeightedShuffle {
79

8-
private WeightedShuffle() {
10+
private
11+
WeightedShuffle() {
912
// Prevent instantiation
1013
}
1114

@@ -16,7 +19,8 @@ private WeightedShuffle() {
1619
* @param array the input array to shuffle
1720
* @param weights the weights for each corresponding element in the array
1821
*/
19-
public static void weightedShuffle(int[] array, int[] weights) {
22+
public
23+
static void weightedShuffle(int[] array, int[] weights) {
2024
// Edge case: Check if weights match the array size
2125
if (array == null || weights == null || array.length != weights.length) {
2226
return;
@@ -27,7 +31,12 @@ public static void weightedShuffle(int[] array, int[] weights) {
2731
indices[i] = i;
2832
}
2933

30-
Arrays.sort(indices, Comparator.comparingInt(i -> - weights[i]));
34+
Random random = new Random();
35+
36+
// Sort indices by weights in descending order, with a random factor for
37+
// ties
38+
Arrays.sort(indices, Comparator.<Integer>comparingInt(i->- weights[i])
39+
.thenComparingInt(i->random.nextInt()));
3140

3241
int[] result = new int[array.length];
3342
for (int i = 0; i < array.length; i++) {
@@ -37,7 +46,8 @@ public static void weightedShuffle(int[] array, int[] weights) {
3746
System.arraycopy(result, 0, array, 0, array.length);
3847
}
3948

40-
public static void main(String[] args) {
49+
public
50+
static void main(String[] args) {
4151
int[] array = {10, 20, 30};
4252
int[] weights = {1, 3, 2};
4353
weightedShuffle(array, weights);

0 commit comments

Comments
 (0)