Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f43c5de
added shuffle algorithms, it's implementation and problem related to it
Darshan-Baligeri Oct 27, 2024
ddcac94
clang-formatted the code
Darshan-Baligeri Oct 27, 2024
dc82551
checked with coding style issues
Darshan-Baligeri Oct 27, 2024
0357b9a
refined code style issues and checked with clang format
Darshan-Baligeri Oct 27, 2024
ead8be5
resolved code style violations
Darshan-Baligeri Oct 27, 2024
0160f11
done required changes
Darshan-Baligeri Oct 27, 2024
545aeae
resolved issues
Darshan-Baligeri Oct 27, 2024
9f38af6
resolved errors
Darshan-Baligeri Oct 27, 2024
1493800
resolves problems
Darshan-Baligeri Oct 27, 2024
e518ce8
resolved issues
Darshan-Baligeri Oct 27, 2024
07276fc
resolved errors
Darshan-Baligeri Oct 27, 2024
8a18e1a
resolved errors
Darshan-Baligeri Oct 27, 2024
6f2c907
resolved errors
Darshan-Baligeri Oct 27, 2024
d404af1
resolved all issues
Darshan-Baligeri Oct 27, 2024
f4169c4
resolved spot bugs
Darshan-Baligeri Oct 27, 2024
213ec92
resolved more spotbugs
Darshan-Baligeri Oct 27, 2024
0cce10d
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
c1775b4
resolved spotbug
Darshan-Baligeri Oct 27, 2024
3e2564a
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
781ec96
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
7eeb225
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
6f3fb36
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
26dd671
resolved spotbugs
Darshan-Baligeri Oct 27, 2024
21d5a3c
resolved build errors
Darshan-Baligeri Oct 27, 2024
4bdab42
resolved bugs
Darshan-Baligeri Oct 27, 2024
a85fd14
resolved error in binary tree
Darshan-Baligeri Oct 27, 2024
293341e
resolved errorin binary tree remove method
Darshan-Baligeri Oct 27, 2024
30f0573
rewrote test cases for binary tree and resolved bugs
Darshan-Baligeri Oct 27, 2024
02945d6
rewrote test cases for binary tree and resolved bugs
Darshan-Baligeri Oct 27, 2024
31601ba
resolved bugs
Darshan-Baligeri Oct 27, 2024
48c1fbc
resolved bugs
Darshan-Baligeri Oct 27, 2024
e7839d4
formatted according to clang-format
Darshan-Baligeri Oct 27, 2024
9a95623
reformatted code
Darshan-Baligeri Oct 27, 2024
cb03bc9
reformatted code
Darshan-Baligeri Oct 27, 2024
c2d5827
applied clang format
Darshan-Baligeri Oct 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.shufflealogrithm;

import java.util.Collections;
import java.util.ArrayList;
import java.util.List;

public class ConstrainedShuffle {

/**
* Shuffles the array so that no element stays in its original position.
*
* @param array the input array to shuffle with constraints
*/
public static void constrainedShuffle(int[] array) {
// Edge case: Check if array has only one element (no valid shuffle possible)
if (array == null || array.length <= 1) return;

List<Integer> shuffledList = new ArrayList<>();
for (int num : array) shuffledList.add(num);

do {
Collections.shuffle(shuffledList);
} while (!isValidShuffle(array, shuffledList));

for (int i = 0; i < array.length; i++) array[i] = shuffledList.get(i);
}

/**
* Verifies that no element is in its original position in the shuffled list.
*/
private static boolean isValidShuffle(int[] original, List<Integer> shuffled) {
for (int i = 0; i < original.length; i++) {
if (original[i] == shuffled.get(i)) return false;
}
return true;
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4};
constrainedShuffle(array);

System.out.println("Constrained Shuffled Array:");
for (int num : array) {
System.out.print(num + " ");
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thealgorithms.shufflealogrithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GroupShuffle {

/**
* Divides the array into k equal-sized groups and shuffles each group separately.
* Returns an empty list if the group count exceeds the array length.
*
* @param array the input array to split into groups
* @param k the number of groups to create
* @return a list of groups, where each group is a shuffled sublist of the input array
*/
public static List<List<Integer>> groupShuffle(int[] array, int k) {
List<List<Integer>> groups = new ArrayList<>();

// Edge case: Check if grouping is possible
if (k > array.length || k <= 0) return groups;

List<Integer> shuffledList = new ArrayList<>();
for (int num : array) shuffledList.add(num);
Collections.shuffle(shuffledList);

int groupSize = array.length / k;
int remainder = array.length % k;

// Distribute elements into k groups
for (int i = 0; i < k; i++) {
int end = (i + 1) * groupSize + (i < remainder ? 1 : 0);
groups.add(new ArrayList<>(shuffledList.subList(i * groupSize + Math.min(i, remainder), end)));
}

return groups;
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
int k = 3;
List<List<Integer>> groups = groupShuffle(array, k);

System.out.println("Shuffled Groups:");
for (List<Integer> group : groups) {
System.out.println(group);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.thealgorithms.shufflealogrithm;

import java.util.Random;

public class ShuffleByRange {

/**
* Shuffles elements within a specified index range, leaving elements outside this range unchanged.
*
* @param array the input array to shuffle
* @param start starting index of the range to shuffle
* @param end ending index of the range to shuffle
*/
public static void shuffleRange(int[] array, int start, int end) {
// Edge case handling
if (array == null || start < 0 || end >= array.length || start >= end) return;

Random random = new Random();

// Shuffle elements only in the specified range
for (int i = end; i > start; i--) {
int j = start + random.nextInt(i - start + 1);
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

public static void main(String[] args) {
int[] array = {10, 20, 30, 40, 50, 60};
int start = 1;
int end = 4;
shuffleRange(array, start, end);

System.out.println("Array after shuffling range:");
for (int num : array) {
System.out.print(num + " ");
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.thealgorithms.shufflealogrithm;

import java.util.Random;

public class UnderstandingShuffleAlgo {

/**
* Shuffles the elements in the array randomly.
* Uses a method that gives each item an equal chance to appear in any position.
*
* @param array the array to be shuffled
*/
public static void shuffle(int[] array) {
// Create a Random object to generate random numbers
Random random = new Random();

// Loop from the last element to the second element
for (int i = array.length - 1; i > 0; i--) {
// Generate a random index from 0 to i (inclusive)
int j = random.nextInt(i + 1);

// Swap the elements at positions i and j
int temp = array[i]; // Temporarily store the element at i
array[i] = array[j]; // Move element from j to i
array[j] = temp; // Place the stored element in position j
}
}

/**
* Main method to demonstrate the shuffle function.
* Shows the array before and after shuffling.
*
* @param args command-line arguments (not used here)
*/
public static void main(String[] args) {
// Create an example array of numbers from 1 to 9
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};

// Display the original array
System.out.println("Original Array:");
for (int num : array) {
System.out.print(num + " ");
}

// Call the shuffle method to randomize the array
shuffle(array);

// Display the shuffled array
System.out.println("\nShuffled Array:");
for (int num : array) {
System.out.print(num + " ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.thealgorithms.shufflealogrithm;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UniquePairShuffle {

/**
* Pairs each element in the array with another element randomly, ensuring no pair repeats.
* If the array length is odd, pairing cannot be completed, so an empty list is returned.
*
* @param array the input array to pair elements from
* @return a list of unique pairs where each pair is represented as an integer array of length 2
*/
public static List<int[]> pairShuffle(int[] array) {
List<int[]> pairs = new ArrayList<>();

// Handle edge case: If the array length is odd, pairing is not possible
if (array.length % 2 != 0) return pairs;

List<Integer> shuffledList = new ArrayList<>();
for (int num : array) shuffledList.add(num);

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

// Form pairs from the shuffled elements
for (int i = 0; i < shuffledList.size(); i += 2) {
pairs.add(new int[]{shuffledList.get(i), shuffledList.get(i + 1)});
}

return pairs;
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4};
List<int[]> pairs = pairShuffle(array);

System.out.println("Generated Unique Pairs:");
for (int[] pair : pairs) {
System.out.println(pair[0] + " - " + pair[1]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.thealgorithms.shufflealogrithm;

import java.util.Arrays;
import java.util.Comparator;

public class WeightedShuffle {

/**
* Shuffles elements based on their weights. Higher weight elements are more likely to appear earlier.
*
* @param array the input array to shuffle
* @param weights the weights for each corresponding element in the array
*/
public static void weightedShuffle(int[] array, int[] weights) {
// Edge case: Check if weights match the array size
if (array == null || weights == null || array.length != weights.length) return;

Integer[] indices = new Integer[array.length];
for (int i = 0; i < array.length; i++) indices[i] = i;

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

int[] result = new int[array.length];
for (int i = 0; i < array.length; i++) result[i] = array[indices[i]];

System.arraycopy(result, 0, array, 0, array.length);
}

public static void main(String[] args) {
int[] array = {10, 20, 30};
int[] weights = {1, 3, 2};
weightedShuffle(array, weights);

System.out.println("Weighted Shuffled Array:");
for (int num : array) {
System.out.print(num + " ");
}
}
}

Loading