Skip to content

Commit ddcac94

Browse files
clang-formatted the code
1 parent f43c5de commit ddcac94

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

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

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

3-
import java.util.Collections;
43
import java.util.ArrayList;
4+
import java.util.Collections;
55
import java.util.List;
66

77
public class ConstrainedShuffle {
@@ -13,24 +13,32 @@ public class ConstrainedShuffle {
1313
*/
1414
public static void constrainedShuffle(int[] array) {
1515
// Edge case: Check if array has only one element (no valid shuffle possible)
16-
if (array == null || array.length <= 1) return;
16+
if (array == null || array.length <= 1) {
17+
return;
18+
}
1719

1820
List<Integer> shuffledList = new ArrayList<>();
19-
for (int num : array) shuffledList.add(num);
21+
for (int num : array) {
22+
shuffledList.add(num);
23+
}
2024

2125
do {
2226
Collections.shuffle(shuffledList);
2327
} while (!isValidShuffle(array, shuffledList));
2428

25-
for (int i = 0; i < array.length; i++) array[i] = shuffledList.get(i);
29+
for (int i = 0; i < array.length; i++) {
30+
array[i] = shuffledList.get(i);
31+
}
2632
}
2733

2834
/**
2935
* Verifies that no element is in its original position in the shuffled list.
3036
*/
3137
private static boolean isValidShuffle(int[] original, List<Integer> shuffled) {
3238
for (int i = 0; i < original.length; i++) {
33-
if (original[i] == shuffled.get(i)) return false;
39+
if (original[i] == shuffled.get(i)) {
40+
return false;
41+
}
3442
}
3543
return true;
3644
}
@@ -45,4 +53,3 @@ public static void main(String[] args) {
4553
}
4654
}
4755
}
48-

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ public static List<List<Integer>> groupShuffle(int[] array, int k) {
1818
List<List<Integer>> groups = new ArrayList<>();
1919

2020
// Edge case: Check if grouping is possible
21-
if (k > array.length || k <= 0) return groups;
21+
if (k > array.length || k <= 0) {
22+
return groups;
23+
}
2224

2325
List<Integer> shuffledList = new ArrayList<>();
24-
for (int num : array) shuffledList.add(num);
26+
for (int num : array) {
27+
shuffledList.add(num);
28+
}
2529
Collections.shuffle(shuffledList);
2630

2731
int groupSize = array.length / k;
@@ -47,4 +51,3 @@ public static void main(String[] args) {
4751
}
4852
}
4953
}
50-

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ public class ShuffleByRange {
1313
*/
1414
public static void shuffleRange(int[] array, int start, int end) {
1515
// Edge case handling
16-
if (array == null || start < 0 || end >= array.length || start >= end) return;
16+
if (array == null || start < 0 || end >= array.length || start >= end) {
17+
return;
18+
}
1719

1820
Random random = new Random();
1921

@@ -38,4 +40,3 @@ public static void main(String[] args) {
3840
}
3941
}
4042
}
41-

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public static void shuffle(int[] array) {
2020
int j = random.nextInt(i + 1);
2121

2222
// Swap the elements at positions i and j
23-
int temp = array[i]; // Temporarily store the element at i
24-
array[i] = array[j]; // Move element from j to i
25-
array[j] = temp; // Place the stored element in position j
23+
int temp = array[i]; // Temporarily store the element at i
24+
array[i] = array[j]; // Move element from j to i
25+
array[j] = temp; // Place the stored element in position j
2626
}
2727
}
2828

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public static List<int[]> pairShuffle(int[] array) {
2020
if (array.length % 2 != 0) return pairs;
2121

2222
List<Integer> shuffledList = new ArrayList<>();
23-
for (int num : array) shuffledList.add(num);
23+
for (int num : array) {
24+
shuffledList.add(num);
25+
}
2426

2527
// Shuffle elements to create random pairs
2628
Collections.shuffle(shuffledList);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public static void weightedShuffle(int[] array, int[] weights) {
1616
if (array == null || weights == null || array.length != weights.length) return;
1717

1818
Integer[] indices = new Integer[array.length];
19-
for (int i = 0; i < array.length; i++) indices[i] = i;
19+
for (int i = 0; i < array.length; i++) {
20+
indices[i] = i;
21+
}
2022

2123
Arrays.sort(indices, Comparator.comparingInt(i -> -weights[i]));
2224

2325
int[] result = new int[array.length];
24-
for (int i = 0; i < array.length; i++) result[i] = array[indices[i]];
26+
for (int i = 0; i < array.length; i++) {
27+
result[i] = array[indices[i]];
28+
}
2529

2630
System.arraycopy(result, 0, array, 0, array.length);
2731
}
@@ -37,4 +41,3 @@ public static void main(String[] args) {
3741
}
3842
}
3943
}
40-

0 commit comments

Comments
 (0)