From c9db5b0552cd5806b53b73c846de850a1bd3726f Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 29 Jul 2024 21:38:58 +0200 Subject: [PATCH 01/10] feat: FlashSort implementation --- .../com/thealgorithms/sorts/FlashSort.java | 185 ++++++++++++++++++ .../thealgorithms/sorts/FlashSortTest.java | 8 + 2 files changed, 193 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/FlashSort.java create mode 100644 src/test/java/com/thealgorithms/sorts/FlashSortTest.java diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java new file mode 100644 index 000000000000..2e9401fb7e28 --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -0,0 +1,185 @@ +package com.thealgorithms.sorts; + +/** + * Implementation of Flash Sort algorithm that implements the SortAlgorithm interface. + * + * Sorts an array using the Flash Sort algorithm. + *

+ * Flash Sort is a distribution sorting algorithm that partitions the data into + * different classes based on a classification array. It performs the sorting by + * first distributing the data elements into different buckets (or classes) and + * then permuting these buckets into the sorted order. + *

+ * The method works as follows: + *

    + *
  1. Finds the minimum and maximum values in the array.
  2. + *
  3. Initializes a classification array `L` to keep track of the number of elements in each class.
  4. + *
  5. Computes a normalization constant `c1` to map elements into classes.
  6. + *
  7. Classifies each element of the array into the corresponding bucket in the classification array.
  8. + *
  9. Transforms the classification array to compute the starting indices of each bucket.
  10. + *
  11. Permutes the elements of the array into sorted order based on the classification.
  12. + *
  13. Uses insertion sort for the final arrangement to ensure complete sorting.
  14. + *
+ */ +public class FlashSort implements SortAlgorithm { + private static final double CLASSIFICATION_RATIO = 0.45; + + /** + * Sorts an array using the Flash Sort algorithm. + * + * @param array the array to be sorted. + * @param the type of elements to be sorted, must be comparable. + * @return the sorted array. + */ + @Override + public > T[] sort(T[] array) { + flashSort(array); + return array; + } + + /** + * Sorts an array using the Flash Sort algorithm. + * + * @param arr the array to be sorted. + * @param the type of elements to be sorted, must be comparable. + */ + private > void flashSort(T[] arr) { + if (arr.length == 0) { + return; + } + + final T min = findMin(arr); + final int maxIndex = findMaxIndex(arr); + + if (arr[maxIndex].compareTo(min) == 0) { + return; // All elements are the same + } + + final int m = (int) (CLASSIFICATION_RATIO * arr.length); + + final int[] L = new int[m]; + + final double c1 = (double) (m - 1) / (arr[maxIndex].compareTo(min)); + + classify(arr, L, c1, min); + + transform(L); + + permute(arr, L, c1, min, arr.length, m); + + insertionSort(arr); + } + + /** + * Finds the minimum value in the array. + * + * @param arr the array to find the minimum value in. + * @param the type of elements in the array, must be comparable. + * @return the minimum value in the array. + */ + private > T findMin(final T[] arr) { + T min = arr[0]; + for (int i = 1; i < arr.length; i++) { + if (arr[i].compareTo(min) < 0) { + min = arr[i]; + } + } + return min; + } + + /** + * Finds the index of the maximum value in the array. + * + * @param arr the array to find the maximum value index in. + * @param the type of elements in the array, must be comparable. + * @return the index of the maximum value in the array. + */ + private > int findMaxIndex(final T[] arr) { + int maxIndex = 0; + for (int i = 1; i < arr.length; i++) { + if (arr[i].compareTo(arr[maxIndex]) > 0) { + maxIndex = i; + } + } + return maxIndex; + } + + /** + * Classifies elements of the array into the classification array L. + * + * @param arr the array to be classified. + * @param L the classification array holding the count of elements in each class. + * @param c1 the normalization constant used to map the elements to the classification array. + * @param min the minimum value in the array. + * @param the type of elements in the array, must be comparable. + */ + private > void classify(final T[] arr, final int[] L, final double c1, final T min) { + for (int i = 0; i < arr.length; i++) { + int k = (int) (c1 * (arr[i].compareTo(min))); + L[k]++; + } + } + + /** + * Transforms the classification array L into the starting index array. + * + * @param L the classification array holding the count of elements in each class. + */ + private void transform(final int[] L) { + for (int i = 1; i < L.length; i++) { + L[i] += L[i - 1]; + } + } + + /** + * Permutes the array into sorted order based on the classification array L. + * + * @param arr the array to be permuted. + * @param L the classification array holding the count of elements in each class. + * @param c1 the normalization constant used to map the elements to the classification array. + * @param min the minimum value in the array. + * @param n the length of the array. + * @param m the number of classes in the classification array. + * @param the type of elements in the array, must be comparable. + */ + private > void permute(final T[] arr, final int[] L, final double c1, T min, int n, int m) { + int move = 0; + int j = 0; + int k = m - 1; + T flash; + while (move < n - 1) { + while (j > L[k] - 1) { + j++; + k = (int) (c1 * (arr[j].compareTo(min))); + } + flash = arr[j]; + while (j != L[k]) { + k = (int) (c1 * (flash.compareTo(min))); + T temp = arr[L[k] - 1]; + arr[L[k] - 1] = flash; + flash = temp; + L[k]--; + move++; + } + } + } + + /** + * Sorts an array using the insertion sort algorithm. + * + * @param arr the array to be sorted. + * @param the type of elements to be sorted, must be comparable. + */ + private > void insertionSort(final T[] arr) { + int n = arr.length; + for (int i = 1; i < n; i++) { + T key = arr[i]; + int j = i - 1; + while (j >= 0 && arr[j].compareTo(key) > 0) { + arr[j + 1] = arr[j]; + j--; + } + arr[j + 1] = key; + } + } +} diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java new file mode 100644 index 000000000000..c6a7a4bff4cc --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -0,0 +1,8 @@ +package com.thealgorithms.sorts; + +public class FlashSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new FlashSort(); + } +} From 5b6e6c94b3369b334d3a555d8d5a9d9c208e3b84 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 29 Jul 2024 21:46:52 +0200 Subject: [PATCH 02/10] checkstyle: fix naming, from L to classificationArray --- .../com/thealgorithms/sorts/FlashSort.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 2e9401fb7e28..48d76a0e6642 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -57,15 +57,15 @@ private > void flashSort(T[] arr) { final int m = (int) (CLASSIFICATION_RATIO * arr.length); - final int[] L = new int[m]; + final int[] classificationArray = new int[m]; final double c1 = (double) (m - 1) / (arr[maxIndex].compareTo(min)); - classify(arr, L, c1, min); + classify(arr, classificationArray, c1, min); - transform(L); + transform(classificationArray); - permute(arr, L, c1, min, arr.length, m); + permute(arr, classificationArray, c1, min, arr.length, m); insertionSort(arr); } @@ -132,33 +132,33 @@ private void transform(final int[] L) { } /** - * Permutes the array into sorted order based on the classification array L. + * Permutes the array into sorted order based on the classification array classificationArray. * * @param arr the array to be permuted. - * @param L the classification array holding the count of elements in each class. + * @param classificationArray the classification array holding the count of elements in each class. * @param c1 the normalization constant used to map the elements to the classification array. * @param min the minimum value in the array. * @param n the length of the array. * @param m the number of classes in the classification array. * @param the type of elements in the array, must be comparable. */ - private > void permute(final T[] arr, final int[] L, final double c1, T min, int n, int m) { + private > void permute(final T[] arr, final int[] classificationArray, final double c1, T min, int n, int m) { int move = 0; int j = 0; int k = m - 1; T flash; while (move < n - 1) { - while (j > L[k] - 1) { + while (j > classificationArray[k] - 1) { j++; k = (int) (c1 * (arr[j].compareTo(min))); } flash = arr[j]; - while (j != L[k]) { + while (j != classificationArray[k]) { k = (int) (c1 * (flash.compareTo(min))); - T temp = arr[L[k] - 1]; - arr[L[k] - 1] = flash; + T temp = arr[classificationArray[k] - 1]; + arr[classificationArray[k] - 1] = flash; flash = temp; - L[k]--; + classificationArray[k]--; move++; } } From 29da0863b463fa0265c908fba3c82f52a331e8a1 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 29 Jul 2024 21:49:41 +0200 Subject: [PATCH 03/10] checkstyle: fix naming, from L to classificationArray for another method --- .../com/thealgorithms/sorts/FlashSort.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 48d76a0e6642..5c23bebf2dca 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -105,29 +105,29 @@ private > int findMaxIndex(final T[] arr) { } /** - * Classifies elements of the array into the classification array L. + * Classifies elements of the array into the classification array classificationArray. * * @param arr the array to be classified. - * @param L the classification array holding the count of elements in each class. + * @param classificationArray the classification array holding the count of elements in each class. * @param c1 the normalization constant used to map the elements to the classification array. * @param min the minimum value in the array. * @param the type of elements in the array, must be comparable. */ - private > void classify(final T[] arr, final int[] L, final double c1, final T min) { + private > void classify(final T[] arr, final int[] classificationArray, final double c1, final T min) { for (int i = 0; i < arr.length; i++) { int k = (int) (c1 * (arr[i].compareTo(min))); - L[k]++; + classificationArray[k]++; } } /** - * Transforms the classification array L into the starting index array. + * Transforms the classification array classificationArray into the starting index array. * - * @param L the classification array holding the count of elements in each class. + * @param classificationArray the classification array holding the count of elements in each class. */ - private void transform(final int[] L) { - for (int i = 1; i < L.length; i++) { - L[i] += L[i - 1]; + private void transform(final int[] classificationArray) { + for (int i = 1; i < classificationArray.length; i++) { + classificationArray[i] += classificationArray[i - 1]; } } From 2356182d6f357525df6c73b7f81b133a2bc60c6a Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 29 Jul 2024 21:54:34 +0200 Subject: [PATCH 04/10] refactor: DIRECTORY.md update --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7e726f3191c6..e73e653227d5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -501,6 +501,7 @@ * [DualPivotQuickSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java) * [DutchNationalFlagSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/DutchNationalFlagSort.java) * [ExchangeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/ExchangeSort.java) + * [FlashSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/FlashSort.java) * [GnomeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/GnomeSort.java) * [HeapSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/HeapSort.java) * [InsertionSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/InsertionSort.java) @@ -873,6 +874,7 @@ * [DualPivotQuickSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/DualPivotQuickSortTest.java) * [DutchNationalFlagSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/DutchNationalFlagSortTest.java) * [ExchangeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java) + * [FlashSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/FlashSortTest.java) * [GnomeSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/GnomeSortTest.java) * [HeapSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/HeapSortTest.java) * [InsertionSortTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/sorts/InsertionSortTest.java) From 4c76a28c104d5a827e3a64ac17e5d9e893cec37a Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Mon, 29 Jul 2024 22:09:21 +0200 Subject: [PATCH 05/10] refactor: remove redundant parentheses --- src/main/java/com/thealgorithms/sorts/FlashSort.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 5c23bebf2dca..0e0cd8d7bc1e 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -59,7 +59,7 @@ private > void flashSort(T[] arr) { final int[] classificationArray = new int[m]; - final double c1 = (double) (m - 1) / (arr[maxIndex].compareTo(min)); + final double c1 = (double) (m - 1) / arr[maxIndex].compareTo(min); classify(arr, classificationArray, c1, min); @@ -115,7 +115,7 @@ private > int findMaxIndex(final T[] arr) { */ private > void classify(final T[] arr, final int[] classificationArray, final double c1, final T min) { for (int i = 0; i < arr.length; i++) { - int k = (int) (c1 * (arr[i].compareTo(min))); + int k = (int) (c1 * arr[i].compareTo(min)); classificationArray[k]++; } } @@ -150,11 +150,11 @@ private > void permute(final T[] arr, final int[ while (move < n - 1) { while (j > classificationArray[k] - 1) { j++; - k = (int) (c1 * (arr[j].compareTo(min))); + k = (int) (c1 * arr[j].compareTo(min)); } flash = arr[j]; while (j != classificationArray[k]) { - k = (int) (c1 * (flash.compareTo(min))); + k = (int) (c1 * flash.compareTo(min)); T temp = arr[classificationArray[k] - 1]; arr[classificationArray[k] - 1] = flash; flash = temp; From c42e4740c66cfa65a1a2e2ec3e583a49dec4ff2d Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 6 Aug 2024 21:25:58 +0200 Subject: [PATCH 06/10] refactor: adding classificationRatio as parameter --- .../com/thealgorithms/sorts/FlashSort.java | 19 +++++++++++-- .../thealgorithms/sorts/FlashSortTest.java | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 0e0cd8d7bc1e..07808496f973 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -22,7 +22,22 @@ * */ public class FlashSort implements SortAlgorithm { - private static final double CLASSIFICATION_RATIO = 0.45; + private final double classificationRatio; + + public FlashSort() { + classificationRatio = 0.45; + } + + public FlashSort(double classificationRatio) { + if (classificationRatio <= 0 || classificationRatio >= 1) { + throw new IllegalArgumentException("Classification ratio must be between 0 and 1 (exclusive)."); + } + this.classificationRatio = classificationRatio; + } + + public double getClassificationRatio() { + return classificationRatio; + } /** * Sorts an array using the Flash Sort algorithm. @@ -55,7 +70,7 @@ private > void flashSort(T[] arr) { return; // All elements are the same } - final int m = (int) (CLASSIFICATION_RATIO * arr.length); + final int m = (int) (classificationRatio * arr.length); final int[] classificationArray = new int[m]; diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java index c6a7a4bff4cc..5cd6a2dc2ca6 100644 --- a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -1,8 +1,35 @@ package com.thealgorithms.sorts; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class FlashSortTest extends SortingAlgorithmTest { @Override SortAlgorithm getSortAlgorithm() { return new FlashSort(); } + + @Test + public void testDefaultConstructor() { + double defaultRation = 0.45; + FlashSort sorter = new FlashSort(); + assertEquals(defaultRation, sorter.getClassificationRatio()); + } + + @ParameterizedTest + @ValueSource(doubles = {0.1, 0.2, 0.5, 0.9}) + public void testCustomConstructorValidRatio(double ratio) { + FlashSort sorter = new FlashSort(ratio); + assertEquals(ratio, sorter.getClassificationRatio()); + } + + @ParameterizedTest + @ValueSource(doubles = {0, 1, -0.1, 1.1}) + public void testCustomConstructorInvalidRatio(double ratio) { + assertThrows(IllegalArgumentException.class, () -> new FlashSort(ratio)); + } } From 09740a9eb31d755969726fe5e9a53396c800eabb Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 6 Aug 2024 21:31:11 +0200 Subject: [PATCH 07/10] checkstyle: fix incorrect import order --- src/test/java/com/thealgorithms/sorts/FlashSortTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java index 5cd6a2dc2ca6..24b3cefc40a0 100644 --- a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -1,12 +1,12 @@ package com.thealgorithms.sorts; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - public class FlashSortTest extends SortingAlgorithmTest { @Override SortAlgorithm getSortAlgorithm() { From 04b7e42340fa17f0e37d71baa60ea5a2ff88273e Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 6 Aug 2024 22:38:26 +0200 Subject: [PATCH 08/10] refactor: adding tests to test different ratios --- .../com/thealgorithms/sorts/FlashSort.java | 10 +++- .../thealgorithms/sorts/FlashSortTest.java | 58 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 07808496f973..13cf0efb3542 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -22,10 +22,9 @@ * */ public class FlashSort implements SortAlgorithm { - private final double classificationRatio; + private double classificationRatio = 0.45;; public FlashSort() { - classificationRatio = 0.45; } public FlashSort(double classificationRatio) { @@ -39,6 +38,13 @@ public double getClassificationRatio() { return classificationRatio; } + public void setClassificationRatio(double classificationRatio) { + if (classificationRatio <= 0 || classificationRatio >= 1) { + throw new IllegalArgumentException("Classification ratio must be between 0 and 1 (exclusive)."); + } + this.classificationRatio = classificationRatio; + } + /** * Sorts an array using the Flash Sort algorithm. * diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java index 24b3cefc40a0..ab33c7810d83 100644 --- a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -3,14 +3,28 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + public class FlashSortTest extends SortingAlgorithmTest { + private final FlashSort flashSort = new FlashSort(); + + public FlashSort getFlashSort() { + return flashSort; + } + @Override SortAlgorithm getSortAlgorithm() { - return new FlashSort(); + return getFlashSort(); } @Test @@ -32,4 +46,46 @@ public void testCustomConstructorValidRatio(double ratio) { public void testCustomConstructorInvalidRatio(double ratio) { assertThrows(IllegalArgumentException.class, () -> new FlashSort(ratio)); } + + @TestFactory + public Collection dynamicTestsForSorting() { + List dynamicTests = new ArrayList<>(); + double[] ratios = {0.1, 0.2, 0.5, 0.9}; + + for (double ratio : ratios) { + FlashSort sorter = (FlashSort) getSortAlgorithm(); + sorter.setClassificationRatio(ratio); + dynamicTests.addAll(createDynamicTestsForRatio(ratio)); + } + + return dynamicTests; + } + + private Collection createDynamicTestsForRatio(double ratio) { + List dynamicTests = new ArrayList<>(); + for (TestMethod testMethod : getTestMethodsFromSuperClass()) { + dynamicTests.add(DynamicTest.dynamicTest("Ratio: " + ratio + " - Test: " + testMethod.name(), testMethod.executable())); + } + return dynamicTests; + } + + private List getTestMethodsFromSuperClass() { + List testMethods = new ArrayList<>(); + Method[] methods = SortingAlgorithmTest.class.getDeclaredMethods(); + for (Method method : methods) { + if (method.isAnnotationPresent(Test.class)) { + testMethods.add(new TestMethod(() -> { + try { + method.setAccessible(true); + method.invoke(this); + } catch (Exception e) { + throw new RuntimeException(e); + } + }, method.getName())); + } + } + return testMethods; + } + + record TestMethod(Executable executable, String name) {} } From 20d2c5b89b0e62b9fdeb5053486a86a70eeed0b4 Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 6 Aug 2024 22:40:47 +0200 Subject: [PATCH 09/10] checkstyle: fix formatting --- src/main/java/com/thealgorithms/sorts/FlashSort.java | 2 +- .../java/com/thealgorithms/sorts/FlashSortTest.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/FlashSort.java b/src/main/java/com/thealgorithms/sorts/FlashSort.java index 13cf0efb3542..e8dbf8c42742 100644 --- a/src/main/java/com/thealgorithms/sorts/FlashSort.java +++ b/src/main/java/com/thealgorithms/sorts/FlashSort.java @@ -22,7 +22,7 @@ * */ public class FlashSort implements SortAlgorithm { - private double classificationRatio = 0.45;; + private double classificationRatio = 0.45; public FlashSort() { } diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java index ab33c7810d83..f3bd30ae1a5e 100644 --- a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -3,6 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; @@ -10,11 +14,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - public class FlashSortTest extends SortingAlgorithmTest { private final FlashSort flashSort = new FlashSort(); @@ -87,5 +86,6 @@ private List getTestMethodsFromSuperClass() { return testMethods; } - record TestMethod(Executable executable, String name) {} + record TestMethod(Executable executable, String name) { + } } From 9460ab4870b24a1a81ff5ec1fcac2b69caa6751d Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 6 Aug 2024 22:47:20 +0200 Subject: [PATCH 10/10] refactoring: redundant accessing to methods has been removed --- src/test/java/com/thealgorithms/sorts/FlashSortTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java index f3bd30ae1a5e..6b1a74403a59 100644 --- a/src/test/java/com/thealgorithms/sorts/FlashSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/FlashSortTest.java @@ -75,7 +75,6 @@ private List getTestMethodsFromSuperClass() { if (method.isAnnotationPresent(Test.class)) { testMethods.add(new TestMethod(() -> { try { - method.setAccessible(true); method.invoke(this); } catch (Exception e) { throw new RuntimeException(e);