-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinaryinsertionsort.java
43 lines (33 loc) Β· 974 Bytes
/
binaryinsertionsort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Made by Aivan Tuquero
import java.util.Arrays;
class GFG
{
public static void main(String[] args)
{
final int[] arr = { 37, 23, 0, 17, 12, 72,
31, 46, 100, 88, 54 };
new GFG().sort(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
// Driver Code
public void sort(int array[])
{
for (int i = 1; i < array.length; i++)
{
int x = array[i];
// Find location to insert
// using binary search
int j = Math.abs(
Arrays.binarySearch(array, 0,
i, x) + 1);
// Shifting array to one
// location right
System.arraycopy(array, j,
array, j + 1, i - j);
// Placing element at its
// correct location
array[j] = x;
}
}
}