Skip to content

Commit 00ba48e

Browse files
authored
Merge branch 'master' into cleanup_ColumnarTranspositionCipher
2 parents 038bc5b + 91101ec commit 00ba48e

File tree

4 files changed

+45
-100
lines changed

4 files changed

+45
-100
lines changed

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
package com.thealgorithms.sorts;
22

3-
public class BinaryInsertionSort {
3+
/**
4+
* BinaryInsertionSort class implements the SortAlgorithm interface using the binary insertion sort technique.
5+
* Binary Insertion Sort improves upon the simple insertion sort by using binary search to find the appropriate
6+
* location to insert the new element, reducing the number of comparisons in the insertion step.
7+
*/
8+
public class BinaryInsertionSort implements SortAlgorithm {
49

5-
// Binary Insertion Sort method
6-
public int[] binaryInsertSort(int[] array) {
10+
/**
11+
* Sorts the given array using the Binary Insertion Sort algorithm.
12+
*
13+
* @param <T> the type of elements in the array, which must implement the Comparable interface
14+
* @param array the array to be sorted
15+
* @return the sorted array
16+
*/
17+
public <T extends Comparable<T>> T[] sort(T[] array) {
718
for (int i = 1; i < array.length; i++) {
8-
int temp = array[i];
19+
final T temp = array[i];
920
int low = 0;
1021
int high = i - 1;
1122

1223
while (low <= high) {
1324
final int mid = (low + high) >>> 1;
14-
if (temp < array[mid]) {
25+
if (temp.compareTo(array[mid]) < 0) {
1526
high = mid - 1;
1627
} else {
1728
low = mid + 1;
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,27 +1,10 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3+
class BinaryInsertionSortTest extends SortingAlgorithmTest {
4+
private final BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort();
45

5-
import org.junit.jupiter.api.Test;
6-
7-
class BinaryInsertionSortTest {
8-
9-
BinaryInsertionSort bis = new BinaryInsertionSort();
10-
11-
@Test
12-
// valid test case
13-
public void binaryInsertionSortTestNonDuplicate() {
14-
int[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7};
15-
int[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
16-
int[] actResult = bis.binaryInsertSort(array);
17-
assertArrayEquals(expResult, actResult);
18-
}
19-
20-
@Test
21-
public void binaryInsertionSortTestDuplicate() {
22-
int[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6};
23-
int[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9};
24-
int[] actResult = bis.binaryInsertSort(array);
25-
assertArrayEquals(expResult, actResult);
6+
@Override
7+
SortAlgorithm getSortAlgorithm() {
8+
return binaryInsertionSort;
269
}
2710
}
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)