Skip to content

Create a new Binary Insertion Sort Algorithm #3206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/com/thealgorithms/sorts/BinaryInsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.thealgorithms.sorts;
public class BinaryInsertionSort{



// Binary Insertion Sort method
public int[] binaryInsertSort(int[] array){

for(int i = 1; i < array.length; i++){

int temp=array[i];
int low = 0;
int high = i - 1;

while(low <= high){
int mid = (low + high) / 2;
if(temp < array[mid]){
high = mid - 1;
}else{
low = mid + 1;
}
}

for(int j = i; j >= low + 1; j--){
array[j] = array[j - 1];
}

array[low] = temp;
}
return array;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.thealgorithms.sorts;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;



class BinaryInsertionSortTest {
BinaryInsertionSort BIS= 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);
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);
assertArrayEquals(expResult,actResult);
}
}