-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRun.java
56 lines (42 loc) · 981 Bytes
/
Run.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
54
55
56
import java.util.Scanner;
public class Run {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int userquery;
int[] numbers = {23, 45, 756, 12, 43};
int min = 0;
int max = numbers.length;
int index = -1;
System.out.println("Enter the number you would like to search for: ");
userquery = input.nextInt();
while(max >= min)
{
System.out.println("max: "+max+" min: "+min+"index: "+ index);
int mid = (min + max)/2;
if(numbers[mid] > userquery)
{
max = mid + 1;
}//end if
else if(numbers[mid] < userquery)
{
max = mid - 1;
}//end else if
else
{
index = mid;
max = -1;
}//end else
}//end while
if(index != -1)
{
System.out.println("Found "+ userquery + " at position "+ index);
}//end if
else
{
System.out.println(userquery + " was not found!");
}//end else
}//end main
}//end class