Skip to content

Commit a9db842

Browse files
alxkmalxvil02
authored
Refactoring BinaryInsertionSort according to common SortAlgorithm approach (#5239)
* Refactoring BinaryInsertionSort according to common SortAlgorithm approach * Formatting has been fixed * Refactoring tests for BinaryInsertionSort according to SortingAlgorithmTest * Removing redundant tests and improving variable readability --------- Co-authored-by: alx <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent bf4fc3f commit a9db842

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
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,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
}

0 commit comments

Comments
 (0)