forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedInsert.java
More file actions
45 lines (36 loc) · 818 Bytes
/
SortedInsert.java
File metadata and controls
45 lines (36 loc) · 818 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
40
41
42
43
44
45
package BinarySearch;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 03/10/18
* Time - 12:05 AM
*/
public class SortedInsert {
int bSearch(ArrayList<Integer> a, int b){
int start = 0;
int end = a.size()-1;
if(a.get(0) > b){
return 0;
}
if(a.get(end) < b){
return end+1;
}
int mid;
while(start<=end){
mid = start + (end - start)/2;
if(a.get(mid) == b){
return mid;
}
else if(a.get(mid) > b){
end = mid - 1;
}
else{
start = mid + 1;
}
}
return start;
}
public int searchInsert(ArrayList<Integer> a, int b) {
return bSearch(a,b);
}
}