-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
59 lines (48 loc) · 1.48 KB
/
Copy pathBinarySearch.java
File metadata and controls
59 lines (48 loc) · 1.48 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
57
58
59
package quiz;
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("배열의 길이 입력");
int n = scanner.nextInt(); //n = 배열의 길이
int i;
int ary[] = new int[n];
for(i = 0; i < n; i ++) {
System.out.println((i + 1) + "번째 값 입력");
ary[i] = scanner.nextInt();
}
System.out.print("정렬 전 :");
for(i = 0; i< n; i ++) {
System.out.print(ary[i] + " ");
}
System.out.println();
//배열생성 완료
//삽입정렬 알고리즘
for (i = 1; i < n; ++i) {
int now = ary[i];
int j = i - 1;
// ary의 요소 중 now보다 큰 값들을 오른쪽으로 이동시킨다.
while (j >= 0 && ary[j] > now) {
ary[j + 1] = ary[j];
j = j - 1;
}
// j+1 위치에 key 값을 삽입한다.
ary[j + 1] = now;
}
System.out.print("정렬 후 :");
for(i = 0; i< n; i ++) {
System.out.print(ary[i] + " ");
}
System.out.println();
//이진탐색 적용
System.out.println("찾읗 숫자 입력");
int x = scanner.nextInt();
BinarySearchAlgorithm search = new BinarySearchAlgorithm(ary,x);
int ans = search.BinarySearch(ary, x);
if(ans == 0) {
System.out.println("없는 숫자");
}else {
System.out.println(x + "는 배열의" + ans + "번째 index에 있음");
}
}
}