-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryInsertionSort.java
53 lines (44 loc) · 1.48 KB
/
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
44
45
46
47
48
49
50
51
52
53
package kb.sort;
import kb.sort.api.Sortable;
public class BinaryInsertionSort implements Sortable {
/**
* Binary Insertion Sort (Insertion Sort using Binary Search).
* O(n^2) time for the worst case(reversed order), but the number of comparisons are O(n.log(n))
* time for average case.
* O(1) space
*/
@Override
public int[] sort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
int curr = nums[i];
// 1. Use custom method:
int idxToInset = binarySearch(nums, 0, i, curr);
// 2. Use library method:
// int idxToInset = Arrays.binarySearch(nums, 0, i, curr);
// idxToInset = idxToInset >= 0 ? idxToInset : Math.abs(idxToInset) - 1;
for (int j = i; j > idxToInset; j--)
nums[j] = nums[j - 1];
nums[idxToInset] = curr;
}
return nums;
}
/**
* @param from - inclusive
* @param to - exclusive
* @return - index of the lower bound
*/
private int binarySearch(int[] arr, int l, int r, int target) {
if (l >= r)
return r;
int mid = (l + r - 1) / 2;
if (arr[mid] == target)
return mid;
else if (target < arr[mid])
return binarySearch(arr, l, mid, target);
else
return binarySearch(arr, mid + 1, r, target);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}