Repository: TheAlgorithms/Java
Pull request #6026 (2024-10-26 12:49:44)

Feature shuffle array
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `36 lines` in your changes missing coverage. Please review.
> Project coverage is 70.42%. Comparing base [(`9e1dd86`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/9e1dd86a0814cbc142cd43a3571bd46aa8500d6f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`53b3699`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/53b36994752c409feb590d4fbebc8fbce321c267?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...main/java/com/thealgorithms/misc/ShuffleArray.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmisc%2FShuffleArray.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9taXNjL1NodWZmbGVBcnJheS5qYXZh) | 0.00% | [26 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |
| [...ealgorithms/slidingwindow/MinSumKSizeSubarray.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fslidingwindow%2FMinSumKSizeSubarray.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zbGlkaW5nd2luZG93L01pblN1bUtTaXplU3ViYXJyYXkuamF2YQ==) | 0.00% | [10 Missing :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #6026      +/-   ##
============================================
- Coverage     70.57%   70.42%   -0.16%     
+ Complexity     4830     4829       -1     
============================================
  Files           646      648       +2     
  Lines         17487    17523      +36     
  Branches       3368     3372       +4     
============================================
- Hits          12342    12340       -2     
- Misses         4681     4718      +37     
- Partials        464      465       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/6026?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

rashi07dashore: @siriak Please check. Thanks :))

Patch:
diff --git a/src/main/java/com/thealgorithms/misc/ShuffleArray.java b/src/main/java/com/thealgorithms/misc/ShuffleArray.java
new file mode 100644
index 00000000..65d38adf
--- /dev/null
+++ b/src/main/java/com/thealgorithms/misc/ShuffleArray.java
@@ -0,0 +1,38 @@
+package com.thealgorithms.misc;
+
+import java.util.Random;
+
+/**
+ * The Fisher-Yates (Knuth) Shuffle algorithm randomly permutes an array's
+ * elements, ensuring each permutation is equally likely.
+ *
+ * <p>
+ * Worst-case performance O(n)
+ * Best-case performance O(n)
+ * Average performance O(n)
+ * Worst-case space complexity O(1)
+ *
+ * This class provides a static method to shuffle an array in place.
+ *
+ * @author Rashi Dashore (https://github.com/rashi07dashore)
+ */
+public final class ShuffleArray {
+    // Prevent instantiation
+    private ShuffleArray() {
+    }
+
+    /**
+     * This method shuffles an array using the Fisher-Yates algorithm.
+     *
+     * @param arr is the input array to be shuffled
+     */
+    public static void shuffle(int[] arr) {
+        Random random = new Random();
+        for (int i = arr.length - 1; i > 0; i--) {
+            int j = random.nextInt(i + 1);
+            int temp = arr[i];
+            arr[i] = arr[j];
+            arr[j] = temp;
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java
new file mode 100644
index 00000000..915b83e3
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/ShuffleArrayTest.java
@@ -0,0 +1,84 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+
+public class ShuffleArrayTest {
+
+    @Test
+    void testShuffleBasic() {
+        int[] arr = {1, 2, 3, 4, 5};
+        int[] originalArr = arr.clone(); // Clone original array for comparison
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array is not the same as the original
+        assertNotEquals(originalArr, arr);
+    }
+
+    @Test
+    void testShuffleSingleElement() {
+        int[] arr = {1};
+        int[] originalArr = arr.clone();
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array is the same as the original
+        assertArrayEquals(originalArr, arr);
+    }
+
+    @Test
+    void testShuffleTwoElements() {
+        int[] arr = {1, 2};
+        int[] originalArr = arr.clone();
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array is not the same as the original
+        assertNotEquals(originalArr, arr);
+        // Check that the shuffled array still contains the same elements
+        assertTrue(arr[0] == 1 || arr[0] == 2);
+        assertTrue(arr[1] == 1 || arr[1] == 2);
+    }
+
+    @Test
+    void testShuffleEmptyArray() {
+        int[] arr = {};
+        int[] originalArr = arr.clone();
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array is the same as the original (still empty)
+        assertArrayEquals(originalArr, arr);
+    }
+
+    @Test
+    void testShuffleLargeArray() {
+        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        int[] originalArr = arr.clone();
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array is not the same as the original
+        assertNotEquals(originalArr, arr);
+    }
+
+    @Test
+    void testShuffleRetainsElements() {
+        int[] arr = {1, 2, 3, 4, 5};
+        ShuffleArray.shuffle(arr);
+
+        // Check that the shuffled array contains the same elements
+        assertTrue(arr.length == 5);
+        for (int i = 1; i <= 5; i++) {
+            assertTrue(contains(arr, i));
+        }
+    }
+
+    private boolean contains(int[] arr, int value) {
+        for (int num : arr) {
+            if (num == value) {
+                return true;
+            }
+        }
+        return false;
+    }
+}