|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
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 { |
30 | 11 |
|
31 | 12 | /**
|
32 |
| - * Odd Even Sort algorithms implements |
| 13 | + * Sorts the given array using the Odd-Even Sort algorithm. |
33 | 14 | *
|
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 |
35 | 18 | */
|
36 |
| - public static void oddEvenSort(int[] arr) { |
| 19 | + @Override |
| 20 | + public <T extends Comparable<T>> T[] sort(T[] arr) { |
37 | 21 | boolean sorted = false;
|
38 | 22 | while (!sorted) {
|
39 | 23 | sorted = true;
|
40 | 24 |
|
41 | 25 | 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); |
44 | 28 | sorted = false;
|
45 | 29 | }
|
46 | 30 | }
|
47 | 31 |
|
48 | 32 | 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); |
51 | 35 | sorted = false;
|
52 | 36 | }
|
53 | 37 | }
|
54 | 38 | }
|
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; |
68 | 40 | }
|
69 | 41 | }
|
0 commit comments