Refactoring and code improving for OddEvenSort (#5242)

* Refactoring and code improving for OddEvenSort, changing it according to SortAlgorithm approach, also applying SortUtils

* Fix checkstyle

* Remove redundant main, remove redundant tests.

---------

Co-authored-by: alx <alx@alx.com>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Alex K 2024-06-20 09:26:09 +03:00 committed by GitHub
parent a9db8428b2
commit 91101ec424
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 73 deletions

View File

@ -1,69 +1,41 @@
package com.thealgorithms.sorts; package com.thealgorithms.sorts;
import java.util.Random; /**
* OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
// https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort * Odd-even sort is a comparison sort related to bubble sort.
public final class OddEvenSort { * 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.
private OddEvenSort() { * The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
} *
*/
public static void main(String[] args) { public final class OddEvenSort implements SortAlgorithm {
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];
}
}
/** /**
* 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; boolean sorted = false;
while (!sorted) { while (!sorted) {
sorted = true; sorted = true;
for (int i = 1; i < arr.length - 1; i += 2) { for (int i = 1; i < arr.length - 1; i += 2) {
if (arr[i] > arr[i + 1]) { if (arr[i].compareTo(arr[i + 1]) > 0) {
swap(arr, i, i + 1); SortUtils.swap(arr, i, i + 1);
sorted = false; sorted = false;
} }
} }
for (int i = 0; i < arr.length - 1; i += 2) { for (int i = 0; i < arr.length - 1; i += 2) {
if (arr[i] > arr[i + 1]) { if (arr[i].compareTo(arr[i + 1]) > 0) {
swap(arr, i, i + 1); SortUtils.swap(arr, i, i + 1);
sorted = false; sorted = false;
} }
} }
} }
} return arr;
/**
* 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;
} }
} }

View File

@ -1,36 +1,15 @@
package com.thealgorithms.sorts; package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
/** /**
* @author Tabbygray (https://github.com/Tabbygray) * @author Tabbygray (https://github.com/Tabbygray)
* @see OddEvenSort * @see OddEvenSort
*/ */
public class OddEvenSortTest { public class OddEvenSortTest extends SortingAlgorithmTest {
@Test private final OddEvenSort oddEvenSort = new OddEvenSort();
public void oddEvenSortEmptyArray() {
int[] inputArray = {};
OddEvenSort.oddEvenSort(inputArray);
int[] expectedOutput = {};
assertArrayEquals(inputArray, expectedOutput);
}
@Test @Override
public void oddEvenSortNaturalNumberArray() { SortAlgorithm getSortAlgorithm() {
int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67}; return oddEvenSort;
OddEvenSort.oddEvenSort(inputArray);
int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
assertArrayEquals(inputArray, expectedOutput);
}
@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);
} }
} }