File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 순차 탐색 소스코드 구현
6
+ public static int sequantialSearch (int n , String target , String [] arr ) {
7
+ // 각 원소를 하나씩 확인하며
8
+ for (int i = 0 ; i < n ; i ++) {
9
+ System .out .println (arr [i ]);
10
+ // 현재의 원소가 찾고자 하는 원소와 동일한 경우
11
+ if (arr [i ].equals (target )) {
12
+ return i + 1 ; // 현재의 위치 반환 (인덱스는 0부터 시작하므로 1 더하기)
13
+ }
14
+ }
15
+ return -1 ; // 원소를 찾지 못한 경우 -1 반환
16
+ }
17
+
18
+ public static void main (String [] args ) {
19
+ Scanner sc = new Scanner (System .in );
20
+
21
+ System .out .println ("생성할 원소 개수를 입력한 다음 한 칸 띄고 찾을 문자열을 입력하세요." );
22
+ // 원소의 개수
23
+ int n = sc .nextInt ();
24
+ // 찾고자 하는 문자열
25
+ String target = sc .next ();
26
+
27
+ System .out .println ("앞서 적은 원소 개수만큼 문자열을 입력하세요. 구분은 띄어쓰기 한 칸으로 합니다." );
28
+ String [] arr = new String [n ];
29
+ for (int i = 0 ; i < n ; i ++) {
30
+ arr [i ] = sc .next ();
31
+ }
32
+
33
+ // 순차 탐색 수행 결과 출력
34
+ System .out .println (sequantialSearch (n , target , arr ));
35
+ }
36
+
37
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 이진 탐색 소스코드 구현(재귀 함수)
6
+ public static int binarySearch (int [] arr , int target , int start , int end ) {
7
+ if (start > end ) return -1 ;
8
+ int mid = (start + end ) / 2 ;
9
+ // 찾은 경우 중간점 인덱스 반환
10
+ if (arr [mid ] == target ) return mid ;
11
+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12
+ else if (arr [mid ] > target ) return binarySearch (arr , target , start , mid - 1 );
13
+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14
+ else return binarySearch (arr , target , mid + 1 , end );
15
+ }
16
+
17
+ public static void main (String [] args ) {
18
+ Scanner sc = new Scanner (System .in );
19
+
20
+ // 원소의 개수(n)와 찾고자 하는 값(target)을 입력받기
21
+ int n = sc .nextInt ();
22
+ int target = sc .nextInt ();
23
+
24
+ // 전체 원소 입력받기
25
+ int [] arr = new int [n ];
26
+ for (int i = 0 ; i < n ; i ++) {
27
+ arr [i ] = sc .nextInt ();
28
+ }
29
+
30
+ // 이진 탐색 수행 결과 출력
31
+ int result = binarySearch (arr , target , 0 , n - 1 );
32
+ if (result == -1 ) {
33
+ System .out .println ("원소가 존재하지 않습니다." );
34
+ }
35
+ else {
36
+ System .out .println (result + 1 );
37
+ }
38
+ }
39
+
40
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ public class Main {
4
+
5
+ // 이진 탐색 소스코드 구현(반복문)
6
+ public static int binarySearch (int [] arr , int target , int start , int end ) {
7
+ while (start <= end ) {
8
+ int mid = (start + end ) / 2 ;
9
+ // 찾은 경우 중간점 인덱스 반환
10
+ if (arr [mid ] == target ) return mid ;
11
+ // 중간점의 값보다 찾고자 하는 값이 작은 경우 왼쪽 확인
12
+ else if (arr [mid ] > target ) end = mid - 1 ;
13
+ // 중간점의 값보다 찾고자 하는 값이 큰 경우 오른쪽 확인
14
+ else start = mid + 1 ;
15
+ }
16
+ return -1 ;
17
+ }
18
+
19
+ public static void main (String [] args ) {
20
+ Scanner sc = new Scanner (System .in );
21
+
22
+ // 원소의 개수(n)와 찾고자 하는 값(target)을 입력받기
23
+ int n = sc .nextInt ();
24
+ int target = sc .nextInt ();
25
+
26
+ // 전체 원소 입력받기
27
+ int [] arr = new int [n ];
28
+ for (int i = 0 ; i < n ; i ++) {
29
+ arr [i ] = sc .nextInt ();
30
+ }
31
+
32
+ // 이진 탐색 수행 결과 출력
33
+ int result = binarySearch (arr , target , 0 , n - 1 );
34
+ if (result == -1 ) {
35
+ System .out .println ("원소가 존재하지 않습니다." );
36
+ }
37
+ else {
38
+ System .out .println (result + 1 );
39
+ }
40
+ }
41
+
42
+ }
You can’t perform that action at this time.
0 commit comments