Skip to content

Commit 4853ac9

Browse files
author
alx
committed
Refactoring and code improving for OddEvenSort, changing it according to SortAlgorithm approach, also applying SortUtils
1 parent a9db842 commit 4853ac9

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

src/main/java/com/thealgorithms/sorts/OddEvenSort.java

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,62 @@
22

33
import java.util.Random;
44

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-
}
5+
/**
6+
* OddEvenSort class implements the SortAlgorithm interface using the odd-even sort technique.
7+
* Odd-even sort is a comparison sort related to bubble sort.
8+
* 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.
9+
* The next step repeats this process for (even, odd)-indexed pairs. This process continues until the list is sorted.
10+
*
11+
*/
12+
public final class OddEvenSort implements SortAlgorithm {
3013

3114
/**
32-
* Odd Even Sort algorithms implements
15+
* Sorts the given array using the Odd-Even Sort algorithm.
3316
*
34-
* @param arr the array contains elements
17+
* @param <T> the type of elements in the array, which must implement the Comparable interface
18+
* @param arr the array to be sorted
19+
* @return the sorted array
3520
*/
36-
public static void oddEvenSort(int[] arr) {
21+
@Override
22+
public <T extends Comparable<T>> T[] sort(T[] arr) {
3723
boolean sorted = false;
3824
while (!sorted) {
3925
sorted = true;
4026

4127
for (int i = 1; i < arr.length - 1; i += 2) {
42-
if (arr[i] > arr[i + 1]) {
43-
swap(arr, i, i + 1);
28+
if (arr[i].compareTo(arr[i + 1]) > 0) {
29+
SortUtils.swap(arr, i, i + 1);
4430
sorted = false;
4531
}
4632
}
4733

4834
for (int i = 0; i < arr.length - 1; i += 2) {
49-
if (arr[i] > arr[i + 1]) {
50-
swap(arr, i, i + 1);
35+
if (arr[i].compareTo(arr[i + 1]) > 0) {
36+
SortUtils.swap(arr, i, i + 1);
5137
sorted = false;
5238
}
5339
}
5440
}
41+
return arr;
5542
}
5643

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;
44+
public static void main(String[] args) {
45+
final Integer[] arr = new Integer[100];
46+
final Random random = new Random();
47+
48+
// Print out unsorted elements
49+
for (int i = 0; i < arr.length; ++i) {
50+
arr[i] = random.nextInt(100) - 50;
51+
System.out.println(arr[i]);
52+
}
53+
System.out.println("--------------");
54+
final OddEvenSort oddEvenSort = new OddEvenSort();
55+
oddEvenSort.sort(arr);
56+
57+
// Print Sorted elements
58+
for (int i = 0; i < arr.length - 1; ++i) {
59+
System.out.println(arr[i]);
60+
assert arr[i] <= arr[i + 1];
61+
}
6862
}
6963
}

src/test/java/com/thealgorithms/sorts/OddEvenSortTest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,34 @@
99
* @see OddEvenSort
1010
*/
1111

12-
public class OddEvenSortTest {
12+
public class OddEvenSortTest extends SortingAlgorithmTest {
13+
private final OddEvenSort oddEvenSort = new OddEvenSort();
14+
15+
@Override
16+
SortAlgorithm getSortAlgorithm() {
17+
return oddEvenSort;
18+
}
19+
1320
@Test
1421
public void oddEvenSortEmptyArray() {
15-
int[] inputArray = {};
16-
OddEvenSort.oddEvenSort(inputArray);
17-
int[] expectedOutput = {};
22+
Integer[] inputArray = {};
23+
Integer[] expectedOutput = {};
1824
assertArrayEquals(inputArray, expectedOutput);
1925
}
2026

2127
@Test
2228
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};
29+
Integer[] inputArray = {18, 91, 86, 60, 21, 44, 37, 78, 98, 67};
30+
oddEvenSort.sort(inputArray);
31+
Integer[] expectedOutput = {18, 21, 37, 44, 60, 67, 78, 86, 91, 98};
2632
assertArrayEquals(inputArray, expectedOutput);
2733
}
2834

2935
@Test
3036
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};
37+
Integer[] inputArray = {57, 69, -45, 12, -85, 3, -76, 36, 67, -14};
38+
oddEvenSort.sort(inputArray);
39+
Integer[] expectedOutput = {-85, -76, -45, -14, 3, 12, 36, 57, 67, 69};
3440
assertArrayEquals(inputArray, expectedOutput);
3541
}
36-
}
42+
}

0 commit comments

Comments
 (0)