-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberInsertion.java
More file actions
43 lines (35 loc) · 888 Bytes
/
Copy pathNumberInsertion.java
File metadata and controls
43 lines (35 loc) · 888 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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class NumberInsertion {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
numbers.add(60);
numbers.add(70);
numbers.add(80);
numbers.add(90);
numbers.add(100);
usingLinearSearch(numbers);
System.out.println(numbers);
usingBinarySearch(numbers);
System.out.println(numbers);
}
private static void usingBinarySearch(List<Integer> numbers) {
int index = Collections.binarySearch(numbers, 55);
numbers.add(-index-1, 55);
}
private static void usingLinearSearch(List<Integer> numbers) {
int size = numbers.size();
for (int i = 0; i < size; i++) {
if (numbers.get(i) > 55) {
numbers.add(i, 55);
break;
}
}
}
}