-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinSearch.java
More file actions
56 lines (43 loc) · 1.51 KB
/
Copy pathBinSearch.java
File metadata and controls
56 lines (43 loc) · 1.51 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
52
53
54
55
56
package search;
import java.util.Scanner;
// 이진 검색
public class BinSearch {
// 요솟수가 n인 배열 a에서 key와 같은 요소를 이진 검색합니다.
static int binSearch(int[] a, int n, int key) {
int pl = 0; // 검색 범위의 첫 인덱스
int pr = n - 1; // 검색 범위와 끝 인덱스
do {
int pc = (pl + pr) / 2; // 중앙 요소의 인덱스
if(a[pc] == key)
return pc; // 검색 성공
else if(a[pc] < key)
pl = pc + 1; // 검색 범위를 뒤쪽 절반으로 좁힘
else
pr = pc - 1; // 검색 범위를 앞쪽 절반을 좁힘
} while(pl <= pr);
return -1; // 검색 실패
}
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.print("요솟수 : ");
int num = stdIn.nextInt();
int[] x = new int[num];
System.out.println("오름차순으로 입력하세요.");
System.out.print("x[0] : "); // 첫 요소 입력
x[0] = stdIn.nextInt();
for(int i=1; i<num; i++) {
do {
System.out.print("x[" + i + "] :");
x[i] = stdIn.nextInt();
} while(x[i] < x[i - 1]); // 바로 앞의 요소보다 작으면 다시 입력
}
System.out.print("검색할 값 : "); // 키 값을 입력
int ky = stdIn.nextInt();
int idx = binSearch(x, num, ky); // 배열 x에서 값이 ky인 요소를 검색
if(idx == -1)
System.out.println("그 값의 요소가 없습니다.");
else
System.out.println(ky + "은(는) x[" + idx + "]에 있습니다.");
stdIn.close();
}
}