Skip to content

Commit 927e6fd

Browse files
Added ShuffleAlgorithm and respective codes
1 parent bca8d0e commit 927e6fd

12 files changed

+920
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
public
7+
final class ConstrainedShuffle {
8+
9+
private
10+
ConstrainedShuffle() {
11+
// Prevent instantiation
12+
}
13+
14+
/**
15+
* Shuffles elements in the array while ensuring that the first and last
16+
* elements remain fixed.
17+
*
18+
* @param array the input array to shuffle
19+
*/
20+
public
21+
static void constrainedShuffle(int[] array) {
22+
// Edge case: If the array has less than 3 elements, no shuffling can occur
23+
if (array == null || array.length < 3) {
24+
return;
25+
}
26+
27+
Random random = new Random();
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--) {
31+
// Generate a random index between 1 and i (inclusive)
32+
int j = random.nextInt(i - 1) + 1;
33+
34+
// Swap the elements at positions i and j
35+
int temp = array[i];
36+
array[i] = array[j];
37+
array[j] = temp;
38+
}
39+
}
40+
41+
public
42+
static void main(String[] args) {
43+
int[] array = {1, 2, 3, 4, 5};
44+
System.out.println("Original Array: " + Arrays.toString(array));
45+
constrainedShuffle(array);
46+
System.out.println("Constrained Shuffled Array: " + Arrays.toString(array));
47+
}
48+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public
8+
final class GroupShuffle {
9+
10+
private
11+
GroupShuffle() {
12+
// Prevent instantiation
13+
}
14+
15+
/**
16+
* Groups and shuffles elements in the array.
17+
*
18+
* @param array the input array to shuffle
19+
* @param groupSize the size of each group
20+
* @return a list of shuffled groups
21+
*/
22+
public
23+
static List<List<Integer>> groupShuffle(int[] array, int groupSize) {
24+
List<List<Integer>> groups = new ArrayList<>();
25+
26+
// Edge case: Check if the group size is valid
27+
if (array == null || groupSize <= 0) {
28+
return groups;
29+
}
30+
31+
for (int i = 0; i < array.length; i += groupSize) {
32+
List<Integer> group = new ArrayList<>();
33+
for (int j = i; j < Math.min(i + groupSize, array.length); j++) {
34+
group.add(array[j]);
35+
}
36+
groups.add(group);
37+
}
38+
39+
// Shuffle only if the group size is greater than 1
40+
if (groupSize > 1) {
41+
Collections.shuffle(groups);
42+
}
43+
44+
return groups;
45+
}
46+
47+
public
48+
static void main(String[] args) {
49+
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
50+
List<List<Integer>> shuffledGroups = groupShuffle(array, 3);
51+
52+
System.out.println("Shuffled Groups:");
53+
for (List<Integer> group : shuffledGroups) {
54+
System.out.println(group);
55+
}
56+
}
57+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.Random;
4+
5+
public
6+
final class ShuffleByRange {
7+
8+
private
9+
ShuffleByRange() {
10+
// Prevent instantiation
11+
}
12+
13+
/**
14+
* Shuffles elements in the specified range of the array.
15+
*
16+
* @param array the input array to shuffle
17+
* @param start the starting index of the range (inclusive)
18+
* @param end the ending index of the range (exclusive)
19+
*/
20+
public
21+
static void shuffleByRange(int[] array, int start, int end) {
22+
// Edge case: Check if the range is valid
23+
if (array == null || start < 0 || end > array.length || start >= end) {
24+
return;
25+
}
26+
27+
Random random = new Random();
28+
for (int i = end - 1; i > start; i--) {
29+
int j = random.nextInt(i - start + 1) + start;
30+
31+
// Swap the elements at positions i and j
32+
int temp = array[i]; // Temporarily store the element at i
33+
array[i] = array[j]; // Move element from j to i
34+
array[j] = temp; // Place the stored element in position j
35+
}
36+
}
37+
38+
public
39+
static void main(String[] args) {
40+
int[] array = {1, 2, 3, 4, 5, 6};
41+
System.out.println("Original Array: ");
42+
for (int num : array) {
43+
System.out.print(num + " ");
44+
}
45+
46+
shuffleByRange(array, 1, 5);
47+
System.out.println("\nShuffled Array (Range 1 to 5): ");
48+
for (int num : array) {
49+
System.out.print(num + " ");
50+
}
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.Random;
4+
5+
public
6+
final class UnderstandingShuffleAlgo {
7+
8+
private
9+
UnderstandingShuffleAlgo() {
10+
// Prevent instantiation
11+
}
12+
13+
/**
14+
* Shuffles the elements in the array randomly.
15+
* Uses a method that gives each item an equal chance to appear in any
16+
* position.
17+
*
18+
* @param array the array to be shuffled
19+
*/
20+
public
21+
static void shuffle(int[] array) {
22+
// Create a Random object to generate random numbers
23+
Random random = new Random();
24+
25+
// Loop from the last element to the second element
26+
for (int i = array.length - 1; i > 0; i--) {
27+
// Generate a random index from 0 to i (inclusive)
28+
int j = random.nextInt(i + 1);
29+
30+
// Swap the elements at positions i and j
31+
int temp = array[i]; // Temporarily store the element at i
32+
array[i] = array[j]; // Move element from j to i
33+
array[j] = temp; // Place the stored element in position j
34+
}
35+
}
36+
37+
/**
38+
* Main method to demonstrate the shuffle function.
39+
* Shows the array before and after shuffling.
40+
*
41+
* @param args command-line arguments (not used here)
42+
*/
43+
public
44+
static void main(String[] args) {
45+
// Create an example array of numbers from 1 to 9
46+
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
47+
48+
// Display the original array
49+
System.out.println("Original Array:");
50+
for (int num : array) {
51+
System.out.print(num + " ");
52+
}
53+
54+
// Call the shuffle method to randomize the array
55+
shuffle(array);
56+
57+
// Display the shuffled array
58+
System.out.println("\nShuffled Array:");
59+
for (int num : array) {
60+
System.out.print(num + " ");
61+
}
62+
}
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
public
8+
final class UniquePairShuffle {
9+
10+
private
11+
UniquePairShuffle() {
12+
// Prevent instantiation
13+
}
14+
15+
/**
16+
* Pairs each element in the array with another element randomly, ensuring no
17+
* pair repeats. If the array length is odd, pairing cannot be completed, so
18+
* an empty list is returned.
19+
*
20+
* @param array the input array to pair elements from
21+
* @return a list of unique pairs where each pair is represented as an integer
22+
* array of length 2
23+
*/
24+
public
25+
static List<int[]> pairShuffle(int[] array) {
26+
List<int[]> pairs = new ArrayList<>();
27+
28+
// Handle edge case: If the array length is odd, pairing is not possible
29+
if (array.length < 2 || array.length % 2 != 0) {
30+
return pairs;
31+
}
32+
33+
List<Integer> shuffledList = new ArrayList<>();
34+
for (int num : array) {
35+
shuffledList.add(num);
36+
}
37+
38+
// Shuffle elements to create random pairs
39+
Collections.shuffle(shuffledList);
40+
41+
// Form pairs from the shuffled elements
42+
for (int i = 0; i < shuffledList.size(); i += 2) {
43+
pairs.add(new int[]{shuffledList.get(i), shuffledList.get(i + 1)});
44+
}
45+
46+
return pairs;
47+
}
48+
49+
public
50+
static void main(String[] args) {
51+
int[] array = {1, 2, 3, 4};
52+
List<int[]> pairs = pairShuffle(array);
53+
54+
System.out.println("Generated Unique Pairs:");
55+
for (int[] pair : pairs) {
56+
System.out.println(pair[0] + " - " + pair[1]);
57+
}
58+
}
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithms.shufflealgo;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.Random;
6+
7+
public
8+
final class WeightedShuffle {
9+
10+
private
11+
WeightedShuffle() {
12+
// Prevent instantiation
13+
}
14+
15+
/**
16+
* Shuffles elements based on their weights. Higher weight elements are more
17+
* likely to appear earlier.
18+
*
19+
* @param array the input array to shuffle
20+
* @param weights the weights for each corresponding element in the array
21+
*/
22+
public
23+
static void weightedShuffle(int[] array, int[] weights) {
24+
// Edge case: Check if weights match the array size
25+
if (array == null || weights == null || array.length != weights.length) {
26+
return;
27+
}
28+
29+
Integer[] indices = new Integer[array.length];
30+
for (int i = 0; i < array.length; i++) {
31+
indices[i] = i;
32+
}
33+
34+
Random random = new Random();
35+
36+
// 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()));
39+
40+
int[] result = new int[array.length];
41+
for (int i = 0; i < array.length; i++) {
42+
result[i] = array[indices[i]];
43+
}
44+
45+
System.arraycopy(result, 0, array, 0, array.length);
46+
}
47+
48+
public
49+
static void main(String[] args) {
50+
int[] array = {10, 20, 30};
51+
int[] weights = {1, 3, 2};
52+
weightedShuffle(array, weights);
53+
54+
System.out.println("Weighted Shuffled Array:");
55+
for (int num : array) {
56+
System.out.print(num + " ");
57+
}
58+
}
59+
}
60+

0 commit comments

Comments
 (0)