๐ Binary Search (์ด์ง ํ์)
์ด์ง ํ์(Binary Search)์ ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ํน์ ๊ฐ์ ๋น ๋ฅด๊ฒ ์ฐพ๋ ์๊ณ ๋ฆฌ์ฆ์
๋๋ค.
ํ์ ๋ฒ์๋ฅผ ์ ๋ฐ์ฉ ์ค์ฌ๊ฐ๋ฉฐ ์ํ๋ ๊ฐ์ ์ฐพ๊ธฐ ๋๋ฌธ์ ์๊ฐ ๋ณต์ก๋๋ O(log N) ์
๋๋ค.
โ
๋์ ์กฐ๊ฑด
- ๋ฐฐ์ด์ด ๋ฐ๋์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ๋์ด ์์ด์ผ ํ๋ค.
- ์ ๋ ฌ๋์ง ์์ ๋ฐฐ์ด์์ ์ด์ง ํ์์ ์ํํ๋ฉด ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํ ์ ์๋ค.
โฑ๏ธ ์๊ฐ ๋ณต์ก๋ ์ ๋ฆฌ
| ๊ฒฝ์ฐ | ์๊ฐ ๋ณต์ก๋ | ๋น๊ต ํ์ (์ต๋) | ์ค๋ช
| |โโ|โโโโโ|โโโโโโ|โโ| | ์ต์ | O(1) | 1 | mid๊ฐ ์ฒ์๋ถํฐ target์ผ ๋ | | ํ๊ท | O(log N) | logโN | ํ์ ๋ฒ์๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ๋ฐ์ฉ ์ค์ | | ์ต์
| O(log N) | logโN | ๊ฐ์ฅ ๋์ ์๊ฑฐ๋ ์๋ ๊ฒฝ์ฐ๊น์ง ํ์ |
๐ก 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
| import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] nums = {1, 3, 7, 13, 5, 9};
int target_num = 7;
// ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ์ด์ง ํ์ ์ํ
Arrays.sort(nums); // โญ ๋ฐ๋์ ์ ๋ ฌ ํ์
int idx = binarySearch(nums, target_num);
if (idx != -1) {
System.out.println("์ฐพ์ idx : " + idx);
} else {
System.out.println("๊ฐ์ ๋ชป ์ฐพ๊ฒ ๋ค");
}
}
public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) {
return mid; // ๊ฐ ์ฐพ์
} else if (arr[mid] < target) {
left = mid + 1; // ์ค๋ฅธ์ชฝ ํ์
} else {
right = mid - 1; // ์ผ์ชฝ ํ์
}
}
return -1; // ๋ชป ์ฐพ์
}
}
|
๐ก 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
| import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] nums = {1, 3, 7, 13, 5, 9};
int target_num = 7;
// ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ์ด์ง ํ์ ์ํ
Arrays.sort(nums); // โญ ๋ฐ๋์ ์ ๋ ฌ ํ์
int idx = binarySearch(nums, target_num);
if (idx != -1) {
System.out.println("์ฐพ์ idx : " + idx);
} else {
System.out.println("๊ฐ์ ๋ชป ์ฐพ๊ฒ ๋ค");
}
}
public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) {
return mid; // ๊ฐ ์ฐพ์
} else if (arr[mid] < target) {
left = mid + 1; // ์ค๋ฅธ์ชฝ ํ์
} else {
right = mid - 1; // ์ผ์ชฝ ํ์
}
}
return -1; // ๋ชป ์ฐพ์
}
}
|
๐ฏ Lower Bound & Upper Bound
lower_bound
์ upper_bound
๋ ์ ๋ ฌ๋ ๋ฐฐ์ด์์ ํน์ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๊ฒฝ๊ณ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋ ์ด์ง ํ์์ ๋ณํ์
๋๋ค.
โ
๊ฐ๋
์ ์
ํจ์ | ์๋ฏธ |
---|
lower_bound | target ์ด์์ด ์ฒ์ ๋์ค๋ ์์น๋ฅผ ๋ฐํ |
upper_bound | target ์ด๊ณผ๊ฐ ์ฒ์ ๋์ค๋ ์์น๋ฅผ ๋ฐํ |
๐ ๋ ๋ค ๊ฐ์ด ์กด์ฌํ์ง ์๋๋ผ๋, ๊ทธ ๊ฐ์ด โ๋ค์ด๊ฐ ์ ์๋ ์๋ฆฌโ๋ฅผ ๋ฐํํฉ๋๋ค.
์ฆ, ์ฝ์
์์น๋ ๋ฒ์ ๊ตฌ๊ฐ ๋ฑ์ ํ์ฉ๋ฉ๋๋ค.
๐ง ์์ ๋ฐฐ์ด
1
| int[] arr = {1, 3, 3, 5, 7, 9};
|
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
| public class BoundsSearch {
// ์ ๋ ฌ๋ ๋ฐฐ์ด์์ target ์ด์ ์ฒซ ์์น
public static int lowerBound(int[] arr, int target) {
int left = 0;
int right = arr.length;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
// ์ ๋ ฌ๋ ๋ฐฐ์ด์์ target ์ด๊ณผ ์ฒซ ์์น
public static int upperBound(int[] arr, int target) {
int left = 0;
int right = arr.length;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] <= target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public static void main(String[] args) {
int[] arr = {1, 3, 3, 5, 7, 9};
int target = 3;
int lb = lowerBound(arr, target);
int ub = upperBound(arr, target);
System.out.println("lower_bound(3): " + lb); // 1
System.out.println("upper_bound(3): " + ub); // 3
System.out.println("3์ ๊ฐ์: " + (ub - lb)); // 2
}
}
|