-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.Binary Insertion Sort
More file actions
39 lines (33 loc) · 942 Bytes
/
7.Binary Insertion Sort
File metadata and controls
39 lines (33 loc) · 942 Bytes
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
package sorting;
import java.util.*;
import java.io.*;
public class binaryInsertion {
static void binInsertion(int arr[]) {
for (int i = 1; i < arr.length; i++)
{
int x = arr[i];
// Find location to insert
// using binary search
int j = Math.abs(Arrays.binarySearch(arr, 0, i, x) + 1);
// Shifting array to one
// location right
System.arraycopy(arr, j,arr, j + 1, i - j);
// Placing element at its
// correct location
arr[j] = x;
}
}
static void printArray(int arr[]) {
int n=arr.length;
for(int i=0;i<n;i++) {
System.out.print(arr[i]+" ");}
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = { -9,2,5,7,12};
binInsertion(arr);
System.out.println("the sorted array is:");
printArray(arr);
}
}