Skip to content

Commit ead8be5

Browse files
resolved code style violations
1 parent 0357b9a commit ead8be5

File tree

6 files changed

+61
-86
lines changed

6 files changed

+61
-86
lines changed
Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,41 @@
11
package com.thealgorithms.shufflealogrithm;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.List;
3+
import java.util.Arrays;
4+
import java.util.Random;
65

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

98
private ConstrainedShuffle() {
109
// Prevent instantiation
1110
}
1211

1312
/**
14-
* Shuffles the array so that no element stays in its original position.
13+
* Shuffles elements in the array while ensuring that the first and last elements remain fixed.
1514
*
16-
* @param array the input array to shuffle with constraints
15+
* @param array the input array to shuffle
1716
*/
1817
public static void constrainedShuffle(int[] array) {
19-
// Edge case: Check if array has only one element (no valid shuffle possible)
20-
if (array == null || array.length <= 1) {
18+
// Edge case: If the array has less than 3 elements, no shuffling can occur
19+
if (array == null || array.length < 3) {
2120
return;
2221
}
2322

24-
List<Integer> shuffledList = new ArrayList<>();
25-
for (int num : array) {
26-
shuffledList.add(num);
27-
}
28-
29-
do {
30-
Collections.shuffle(shuffledList);
31-
} while (!isValidShuffle(array, shuffledList));
23+
Random random = new Random();
24+
for (int i = array.length - 1; i > 1; i--) {
25+
// Generate a random index between 1 and i (inclusive)
26+
int j = random.nextInt(i - 1) + 1;
3227

33-
for (int i = 0; i < array.length; i++) {
34-
array[i] = shuffledList.get(i);
28+
// Swap the elements at positions i and j
29+
int temp = array[i]; // Temporarily store the element at i
30+
array[i] = array[j]; // Move element from j to i
31+
array[j] = temp; // Place the stored element in position j
3532
}
3633
}
3734

38-
/**
39-
* Verifies that no element is in its original position in the shuffled list.
40-
*/
41-
private static boolean isValidShuffle(int[] original, List<Integer> shuffled) {
42-
for (int i = 0; i < original.length; i++) {
43-
if (original[i] == shuffled.get(i)) {
44-
return false;
45-
}
46-
}
47-
return true;
48-
}
49-
5035
public static void main(String[] args) {
51-
int[] array = {1, 2, 3, 4};
36+
int[] array = {1, 2, 3, 4, 5};
37+
System.out.println("Original Array: " + Arrays.toString(array));
5238
constrainedShuffle(array);
53-
54-
System.out.println("Constrained Shuffled Array:");
55-
for (int num : array) {
56-
System.out.print(num + " ");
57-
}
39+
System.out.println("Constrained Shuffled Array: " + Arrays.toString(array));
5840
}
5941
}

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,46 @@
44
import java.util.Collections;
55
import java.util.List;
66

7-
public class GroupShuffle {
7+
public final class GroupShuffle {
88

99
private GroupShuffle() {
1010
// Prevent instantiation
1111
}
1212

1313
/**
14-
* Divides the array into k equal-sized groups and shuffles each group separately.
15-
* Returns an empty list if the group count exceeds the array length.
14+
* Groups and shuffles elements in the array.
1615
*
17-
* @param array the input array to split into groups
18-
* @param k the number of groups to create
19-
* @return a list of groups, where each group is a shuffled sublist of the input array
16+
* @param array the input array to shuffle
17+
* @param groupSize the size of each group
18+
* @return a list of shuffled groups
2019
*/
21-
public static List<List<Integer>> groupShuffle(int[] array, int k) {
20+
public static List<List<Integer>> groupShuffle(int[] array, int groupSize) {
2221
List<List<Integer>> groups = new ArrayList<>();
2322

24-
// Edge case: Check if grouping is possible
25-
if (k > array.length || k <= 0) {
23+
// Edge case: Check if the group size is valid
24+
if (array == null || groupSize <= 0) {
2625
return groups;
2726
}
2827

29-
List<Integer> shuffledList = new ArrayList<>();
30-
for (int num : array) {
31-
shuffledList.add(num);
32-
}
33-
Collections.shuffle(shuffledList);
34-
35-
int groupSize = array.length / k;
36-
int remainder = array.length % k;
37-
38-
// Distribute elements into k groups
39-
for (int i = 0; i < k; i++) {
40-
int end = (i + 1) * groupSize + (i < remainder ? 1 : 0);
41-
groups.add(new ArrayList<>(shuffledList.subList(i * groupSize + Math.min(i, remainder), end)));
28+
for (int i = 0; i < array.length; i += groupSize) {
29+
List<Integer> group = new ArrayList<>();
30+
for (int j = i; j < Math.min(i + groupSize, array.length); j++) {
31+
group.add(array[j]);
32+
}
33+
groups.add(group);
4234
}
4335

36+
// Shuffle the groups to randomize their order
37+
Collections.shuffle(groups);
4438
return groups;
4539
}
4640

4741
public static void main(String[] args) {
48-
int[] array = {1, 2, 3, 4, 5, 6};
49-
int k = 3;
50-
List<List<Integer>> groups = groupShuffle(array, k);
42+
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
43+
List<List<Integer>> shuffledGroups = groupShuffle(array, 3);
5144

5245
System.out.println("Shuffled Groups:");
53-
for (List<Integer> group : groups) {
46+
for (List<Integer> group : shuffledGroups) {
5447
System.out.println(group);
5548
}
5649
}

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,45 @@
22

33
import java.util.Random;
44

5-
public class ShuffleByRange {
5+
public final class ShuffleByRange {
66

77
private ShuffleByRange() {
88
// Prevent instantiation
99
}
1010

1111
/**
12-
* Shuffles elements within a specified index range, leaving elements outside this range unchanged.
12+
* Shuffles elements in the specified range of the array.
1313
*
1414
* @param array the input array to shuffle
15-
* @param start starting index of the range to shuffle
16-
* @param end ending index of the range to shuffle
15+
* @param start the starting index of the range (inclusive)
16+
* @param end the ending index of the range (exclusive)
1717
*/
18-
public static void shuffleRange(int[] array, int start, int end) {
19-
// Edge case handling
20-
if (array == null || start < 0 || end >= array.length || start >= end) {
18+
public static void shuffleByRange(int[] array, int start, int end) {
19+
// Edge case: Check if the range is valid
20+
if (array == null || start < 0 || end > array.length || start >= end) {
2121
return;
2222
}
2323

2424
Random random = new Random();
25+
for (int i = end - 1; i > start; i--) {
26+
int j = random.nextInt(i - start + 1) + start;
2527

26-
// Shuffle elements only in the specified range
27-
for (int i = end; i > start; i--) {
28-
int j = start + random.nextInt(i - start + 1);
29-
int temp = array[i];
30-
array[i] = array[j];
31-
array[j] = temp;
28+
// Swap the elements at positions i and j
29+
int temp = array[i]; // Temporarily store the element at i
30+
array[i] = array[j]; // Move element from j to i
31+
array[j] = temp; // Place the stored element in position j
3232
}
3333
}
3434

3535
public static void main(String[] args) {
36-
int[] array = {10, 20, 30, 40, 50, 60};
37-
int start = 1;
38-
int end = 4;
39-
shuffleRange(array, start, end);
36+
int[] array = {1, 2, 3, 4, 5, 6};
37+
System.out.println("Original Array: ");
38+
for (int num : array) {
39+
System.out.print(num + " ");
40+
}
4041

41-
System.out.println("Array after shuffling range:");
42+
shuffleByRange(array, 1, 5);
43+
System.out.println("\nShuffled Array (Range 1 to 5): ");
4244
for (int num : array) {
4345
System.out.print(num + " ");
4446
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.Random;
44

5-
public class UnderstandingShuffleAlgo {
5+
public final class UnderstandingShuffleAlgo {
66

77
private UnderstandingShuffleAlgo() {
88
// Prevent instantiation

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

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

7-
public class UniquePairShuffle {
7+
public final class UniquePairShuffle {
88

99
private UniquePairShuffle() {
1010
// Prevent instantiation
@@ -21,9 +21,7 @@ public static List<int[]> pairShuffle(int[] array) {
2121
List<int[]> pairs = new ArrayList<>();
2222

2323
// Handle edge case: If the array length is odd, pairing is not possible
24-
if (array.length % 2 != 0) {
25-
return pairs;
26-
}
24+
if (array.length % 2 != 0) return pairs;
2725

2826
List<Integer> shuffledList = new ArrayList<>();
2927
for (int num : array) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Arrays;
44
import java.util.Comparator;
55

6-
public class WeightedShuffle {
6+
public final class WeightedShuffle {
77

88
private WeightedShuffle() {
99
// Prevent instantiation

0 commit comments

Comments
 (0)