-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.java
More file actions
51 lines (38 loc) · 1.14 KB
/
Binary.java
File metadata and controls
51 lines (38 loc) · 1.14 KB
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
import java.util.Scanner;
public class Binary{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
int[] num = {0};
while(true){
System.out.print("Insert Number: ");
int target = console.nextInt();
boolean found = false;
for(int n : num)
if(n == target) found = true;
if(found)continue;
//Binary Search
int low = 0;
int high = num.length -1;
int mid = -1;
while(low <= high){
mid = low + (high - low) /2;
int val = num[mid];
if(val < target) low = mid+1;
else if(val > target) high = mid -1;
else break;
}
//Condition whether the insert number is greater than the last index
if(num[mid] < target)mid++;
int[] newArr = new int[num.length+1];
for(int i =0; i < newArr.length; i++){
if(i < mid) newArr[i] = num[i];
else if(i == mid) newArr[i] = target;
else newArr[i] = num[i-1];
}
for(int x : newArr)
System.out.print(x + ", ");
num =newArr;
System.out.println();
}
}
}