mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 17:56:02 +08:00
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 <alx@alx.com> Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -1,17 +1,28 @@
|
||||
package com.thealgorithms.sorts;
|
||||
|
||||
public class BinaryInsertionSort {
|
||||
/**
|
||||
* 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
|
||||
public int[] binaryInsertSort(int[] array) {
|
||||
/**
|
||||
* Sorts the given array using the Binary Insertion Sort algorithm.
|
||||
*
|
||||
* @param <T> 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 extends Comparable<T>> T[] sort(T[] array) {
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
int temp = array[i];
|
||||
final 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;
|
||||
|
Reference in New Issue
Block a user