|
2 | 2 |
|
3 | 3 | import java.util.Random;
|
4 | 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 |
| - } |
| 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 { |
30 | 13 |
|
31 | 14 | /**
|
32 |
| - * Odd Even Sort algorithms implements |
| 15 | + * Sorts the given array using the Odd-Even Sort algorithm. |
33 | 16 | *
|
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 |
35 | 20 | */
|
36 |
| - public static void oddEvenSort(int[] arr) { |
| 21 | + @Override |
| 22 | + public <T extends Comparable<T>> T[] sort(T[] arr) { |
37 | 23 | boolean sorted = false;
|
38 | 24 | while (!sorted) {
|
39 | 25 | sorted = true;
|
40 | 26 |
|
41 | 27 | 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); |
44 | 30 | sorted = false;
|
45 | 31 | }
|
46 | 32 | }
|
47 | 33 |
|
48 | 34 | 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); |
51 | 37 | sorted = false;
|
52 | 38 | }
|
53 | 39 | }
|
54 | 40 | }
|
| 41 | + return arr; |
55 | 42 | }
|
56 | 43 |
|
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 | + } |
68 | 62 | }
|
69 | 63 | }
|
0 commit comments