Repository: TheAlgorithms/Java
Pull request #5242 (2024-06-19 19:20:16)

Refactoring and code improving for OddEvenSort
Refactoring and code improving for OddEvenSort, changing it according to SortAlgorithm approach, also applying SortUtils

<!--
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`


Patch:
diff --git a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
index fb6e4d26..8db85f39 100644
--- a/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
+++ b/src/main/java/com/thealgorithms/sorts/OddEvenSort.java
@@ -1,69 +1,41 @@
 package com.thealgorithms.sorts;
 
-import java.util.Random;
-
-// https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
-public final class OddEvenSort {
-    private OddEvenSort() {
-    }
-
-    public static void main(String[] args) {
-        int[] arr = new int[100];
-
-        Random random = new Random();
-
-        // Print out unsorted elements
-        for (int i = 0; i < arr.length; ++i) {
-            arr[i] = random.nextInt(100) - 50;
-            System.out.println(arr[i]);
-        }
-        System.out.println("--------------");
-
-        oddEvenSort(arr);
-
-        // Print Sorted elements
-        for (int i = 0; i < arr.length - 1; ++i) {
-            System.out.println(arr[i]);
-            assert arr[i] <= arr[i + 1];
-        }
-    }
+/**
+ * OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
+ * Odd-even sort is a comparison sort related to bubble sort.
+ * It operates by comparing all (odd, even)-indexed pairs of adjacent elements in the list and, if a pair is in the wrong order, swapping them.
+ * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
+ *
+ */
+public final class OddEvenSort implements SortAlgorithm {
 
     /**
-     * Odd Even Sort algorithms implements
+     * Sorts the given array using the Odd-Even Sort algorithm.
      *
-     * @param arr the array contains elements
+     * @param <T> the type of elements in the array, which must implement the Comparable interface
+     * @param arr the array to be sorted
+     * @return the sorted array
      */
-    public static void oddEvenSort(int[] arr) {
+    @Override
+    public <T extends Comparable<T>> T[] sort(T[] arr) {
         boolean sorted = false;
         while (!sorted) {
             sorted = true;
 
             for (int i = 1; i < arr.length - 1; i += 2) {
-                if (arr[i] > arr[i + 1]) {
-                    swap(arr, i, i + 1);
+                if (arr[i].compareTo(arr[i + 1]) > 0) {
+                    SortUtils.swap(arr, i, i + 1);
                     sorted = false;
                 }
             }
 
             for (int i = 0; i < arr.length - 1; i += 2) {
-                if (arr[i] > arr[i + 1]) {
-                    swap(arr, i, i + 1);
+                if (arr[i].compareTo(arr[i + 1]) > 0) {
+                    SortUtils.swap(arr, i, i + 1);
                     sorted = false;
                 }
             }
         }
-    }
-
-    /**
-     * Helper function to swap two array values.
-     *
-     * @param arr the array contains elements
-     * @param i the first index to be swapped
-     * @param j the second index to be swapped
-     */
-    private static void swap(int[] arr, int i, int j) {
-        int temp = arr[i];
-        arr[i] = arr[j];
-        arr[j] = temp;
+        return arr;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java b/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
index a7d0a58e..09ef8014 100644
--- a/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
+++ b/src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java
@@ -1,36 +1,15 @@
 package com.thealgorithms.sorts;
 
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-
-import org.junit.jupiter.api.Test;
-
 /**
  * @author Tabbygray (https://github.com/Tabbygray)
  * @see OddEvenSort
  */
 
-public class OddEvenSortTest {
-    @Test
-    public void oddEvenSortEmptyArray() {
-        int[] inputArray = {};
-        OddEvenSort.oddEvenSort(inputArray);
-        int[] expectedOutput = {};
-        assertArrayEquals(inputArray, expectedOutput);
-    }
-
-    @Test
-    public void oddEvenSortNaturalNumberArray() {
-        int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
-        OddEvenSort.oddEvenSort(inputArray);
-        int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
-        assertArrayEquals(inputArray, expectedOutput);
-    }
+public class OddEvenSortTest extends SortingAlgorithmTest {
+    private final OddEvenSort oddEvenSort = new OddEvenSort();
 
-    @Test
-    public void oddEvenSortIntegerArray() {
-        int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
-        OddEvenSort.oddEvenSort(inputArray);
-        int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
-        assertArrayEquals(inputArray, expectedOutput);
+    @Override
+    SortAlgorithm getSortAlgorithm() {
+        return oddEvenSort;
     }
 }