From 042773929668112e525ac12247d67e6ac965d940 Mon Sep 17 00:00:00 2001 From: alx Date: Tue, 18 Jun 2024 23:08:01 +0200 Subject: [PATCH 1/4] Refactoring BinaryInsertionSort according to common SortAlgorithm approach --- .../thealgorithms/sorts/BinaryInsertionSort.java | 8 ++++---- .../sorts/BinaryInsertionSortTest.java | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java index 13076c617c76..7b013132cfc8 100644 --- a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java @@ -1,17 +1,17 @@ package com.thealgorithms.sorts; -public class BinaryInsertionSort { +public class BinaryInsertionSort implements SortAlgorithm { // Binary Insertion Sort method - public int[] binaryInsertSort(int[] array) { + public > T[] sort(T[] array) { for (int i = 1; i < array.length; i++) { - int temp = array[i]; + T temp = array[i]; int low = 0; int high = i - 1; while (low <= high) { final int mid = (low + high) >>> 1; - if (temp < array[mid]) { + if (temp.compareTo(array[mid]) < 0 ) { high = mid - 1; } else { low = mid + 1; diff --git a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java index bdd0702942a2..5ed1433e8fdd 100644 --- a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java @@ -6,22 +6,22 @@ class BinaryInsertionSortTest { - BinaryInsertionSort bis = new BinaryInsertionSort(); + BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort(); @Test // valid test case public void binaryInsertionSortTestNonDuplicate() { - int[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7}; - int[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - int[] actResult = bis.binaryInsertSort(array); + Integer[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7}; + Integer[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + Integer[] actResult = binaryInsertionSort.sort(array); assertArrayEquals(expResult, actResult); } @Test public void binaryInsertionSortTestDuplicate() { - int[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6}; - int[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9}; - int[] actResult = bis.binaryInsertSort(array); + Integer[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6}; + Integer[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9}; + Integer[] actResult = binaryInsertionSort.sort(array); assertArrayEquals(expResult, actResult); } } From 76193a68f4210065b534a8d3533a4ab1d34c84fc Mon Sep 17 00:00:00 2001 From: alx Date: Tue, 18 Jun 2024 23:12:01 +0200 Subject: [PATCH 2/4] Formatting has been fixed --- src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java index 7b013132cfc8..fcf45558d71a 100644 --- a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java @@ -11,7 +11,7 @@ public > T[] sort(T[] array) { while (low <= high) { final int mid = (low + high) >>> 1; - if (temp.compareTo(array[mid]) < 0 ) { + if (temp.compareTo(array[mid]) < 0) { high = mid - 1; } else { low = mid + 1; From 1814e2b91e8ac1397fa14aa1101561e5388cb031 Mon Sep 17 00:00:00 2001 From: alx Date: Wed, 19 Jun 2024 12:08:32 +0200 Subject: [PATCH 3/4] Refactoring tests for BinaryInsertionSort according to SortingAlgorithmTest --- .../thealgorithms/sorts/BinaryInsertionSort.java | 13 ++++++++++++- .../sorts/BinaryInsertionSortTest.java | 8 ++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java index fcf45558d71a..a184d02f642b 100644 --- a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java @@ -1,8 +1,19 @@ package com.thealgorithms.sorts; +/** + * BinaryInsertionSort class implements the SortAlgorithm interface using the binary insertion sort technique. + * Binary Insertion Sort improves upon the simple insertion sort by using binary search to find the appropriate + * location to insert the new element, reducing the number of comparisons in the insertion step. + */ public class BinaryInsertionSort implements SortAlgorithm { - // Binary Insertion Sort method + /** + * Sorts the given array using the Binary Insertion Sort algorithm. + * + * @param the type of elements in the array, which must implement the Comparable interface + * @param array the array to be sorted + * @return the sorted array + */ public > T[] sort(T[] array) { for (int i = 1; i < array.length; i++) { T temp = array[i]; diff --git a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java index 5ed1433e8fdd..7c6b5f9858cd 100644 --- a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java @@ -4,9 +4,13 @@ import org.junit.jupiter.api.Test; -class BinaryInsertionSortTest { +class BinaryInsertionSortTest extends SortingAlgorithmTest { + private final BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort(); - BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort(); + @Override + SortAlgorithm getSortAlgorithm() { + return binaryInsertionSort; + } @Test // valid test case From cb4290988d116399833e32ccec2b13a52bb2d14d Mon Sep 17 00:00:00 2001 From: alx Date: Wed, 19 Jun 2024 18:07:27 +0200 Subject: [PATCH 4/4] Removing redundant tests and improving variable readability --- .../sorts/BinaryInsertionSort.java | 2 +- .../sorts/BinaryInsertionSortTest.java | 21 ------------------- 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java index a184d02f642b..b6f5d92e7928 100644 --- a/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java +++ b/src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java @@ -16,7 +16,7 @@ public class BinaryInsertionSort implements SortAlgorithm { */ public > T[] sort(T[] array) { for (int i = 1; i < array.length; i++) { - T temp = array[i]; + final T temp = array[i]; int low = 0; int high = i - 1; diff --git a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java index 7c6b5f9858cd..82cb3ba60987 100644 --- a/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/BinaryInsertionSortTest.java @@ -1,9 +1,5 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import org.junit.jupiter.api.Test; - class BinaryInsertionSortTest extends SortingAlgorithmTest { private final BinaryInsertionSort binaryInsertionSort = new BinaryInsertionSort(); @@ -11,21 +7,4 @@ class BinaryInsertionSortTest extends SortingAlgorithmTest { SortAlgorithm getSortAlgorithm() { return binaryInsertionSort; } - - @Test - // valid test case - public void binaryInsertionSortTestNonDuplicate() { - Integer[] array = {1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7}; - Integer[] expResult = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Integer[] actResult = binaryInsertionSort.sort(array); - assertArrayEquals(expResult, actResult); - } - - @Test - public void binaryInsertionSortTestDuplicate() { - Integer[] array = {1, 1, 1, 5, 9, 8, 7, 2, 6}; - Integer[] expResult = {1, 1, 1, 2, 5, 6, 7, 8, 9}; - Integer[] actResult = binaryInsertionSort.sort(array); - assertArrayEquals(expResult, actResult); - } }