mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Add Binary Insertion Sort (#3206)
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user