Skip to content

Commit 91101ec

Browse files
alxkmalxvil02
authored
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 <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent a9db842 commit 91101ec

File tree

2 files changed

+24
-73
lines changed

2 files changed

+24
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,41 @@
11
package com.thealgorithms.sorts;
22

3-
import java.util.Random;
4-
5-
// https://en.wikipedia.org/wiki/Odd%E2%80%93even_sort
6-
public final class OddEvenSort {
7-
private OddEvenSort() {
8-
}
9-
10-
public static void main(String[] args) {
11-
int[] arr = new int[100];
12-
13-
Random random = new Random();
14-
15-
// Print out unsorted elements
16-
for (int i = 0; i < arr.length; ++i) {
17-
arr[i] = random.nextInt(100) - 50;
18-
System.out.println(arr[i]);
19-
}
20-
System.out.println("--------------");
21-
22-
oddEvenSort(arr);
23-
24-
// Print Sorted elements
25-
for (int i = 0; i < arr.length - 1; ++i) {
26-
System.out.println(arr[i]);
27-
assert arr[i] <= arr[i + 1];
28-
}
29-
}
3+
/**
4+
* OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
5+
* Odd-even sort is a comparison sort related to bubble sort.
6+
* 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.
7+
* The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
8+
*
9+
*/
10+
public final class OddEvenSort implements SortAlgorithm {
3011

3112
/**
32-
* Odd Even Sort algorithms implements
13+
* Sorts the given array using the Odd-Even Sort algorithm.
3314
*
34-
* @param arr the array contains elements
15+
* @param <T> the type of elements in the array, which must implement the Comparable interface
16+
* @param arr the array to be sorted
17+
* @return the sorted array
3518
*/
36-
public static void oddEvenSort(int[] arr) {
19+
@Override
20+
public <T extends Comparable<T>> T[] sort(T[] arr) {
3721
boolean sorted = false;
3822
while (!sorted) {
3923
sorted = true;
4024

4125
for (int i = 1; i < arr.length - 1; i += 2) {
42-
if (arr[i] > arr[i + 1]) {
43-
swap(arr, i, i + 1);
26+
if (arr[i].compareTo(arr[i + 1]) > 0) {
27+
SortUtils.swap(arr, i, i + 1);
4428
sorted = false;
4529
}
4630
}
4731

4832
for (int i = 0; i < arr.length - 1; i += 2) {
49-
if (arr[i] > arr[i + 1]) {
50-
swap(arr, i, i + 1);
33+
if (arr[i].compareTo(arr[i + 1]) > 0) {
34+
SortUtils.swap(arr, i, i + 1);
5135
sorted = false;
5236
}
5337
}
5438
}
55-
}
56-
57-
/**
58-
* Helper function to swap two array values.
59-
*
60-
* @param arr the array contains elements
61-
* @param i the first index to be swapped
62-
* @param j the second index to be swapped
63-
*/
64-
private static void swap(int[] arr, int i, int j) {
65-
int temp = arr[i];
66-
arr[i] = arr[j];
67-
arr[j] = temp;
39+
return arr;
6840
}
6941
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
73
/**
84
* @author Tabbygray (https://github.com/Tabbygray)
95
* @see OddEvenSort
106
*/
117

12-
public class OddEvenSortTest {
13-
@Test
14-
public void oddEvenSortEmptyArray() {
15-
int[] inputArray = {};
16-
OddEvenSort.oddEvenSort(inputArray);
17-
int[] expectedOutput = {};
18-
assertArrayEquals(inputArray, expectedOutput);
19-
}
20-
21-
@Test
22-
public void oddEvenSortNaturalNumberArray() {
23-
int[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
24-
OddEvenSort.oddEvenSort(inputArray);
25-
int[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
26-
assertArrayEquals(inputArray, expectedOutput);
27-
}
8+
public class OddEvenSortTest extends SortingAlgorithmTest {
9+
private final OddEvenSort oddEvenSort = new OddEvenSort();
2810

29-
@Test
30-
public void oddEvenSortIntegerArray() {
31-
int[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
32-
OddEvenSort.oddEvenSort(inputArray);
33-
int[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
34-
assertArrayEquals(inputArray, expectedOutput);
11+
@Override
12+
SortAlgorithm getSortAlgorithm() {
13+
return oddEvenSort;
3514
}
3615
}

0 commit comments

Comments
 (0)