Post

binary search

๐Ÿ” 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; // ๋ชป ์ฐพ์Œ
	}
}
1
์ฐพ์€ idx : 2

๐Ÿ’ก 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; // ๋ชป ์ฐพ์Œ
	}
}
1
์ฐพ์€ idx : 2





๐ŸŽฏ Lower Bound & Upper Bound

lower_bound ์™€ upper_bound๋Š” ์ •๋ ฌ๋œ ๋ฐฐ์—ด์—์„œ ํŠน์ • ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฝ๊ณ„ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ์ด์ง„ ํƒ์ƒ‰์˜ ๋ณ€ํ˜•์ž…๋‹ˆ๋‹ค.


โœ… ๊ฐœ๋… ์ •์˜

ํ•จ์ˆ˜์˜๋ฏธ
lower_boundtarget ์ด์ƒ์ด ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜
upper_boundtarget ์ดˆ๊ณผ๊ฐ€ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜

๐Ÿ“Œ ๋‘˜ ๋‹ค ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋”๋ผ๋„, ๊ทธ ๊ฐ’์ด โ€œ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ž๋ฆฌโ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰, ์‚ฝ์ž… ์œ„์น˜๋‚˜ ๋ฒ”์œ„ ๊ตฌ๊ฐ„ ๋“ฑ์— ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.





๐Ÿง  ์˜ˆ์‹œ ๋ฐฐ์—ด

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
    }
}
This post is licensed under CC BY 4.0 by the author.
3D GIF

Trending Tags

๋„์›€์ด ํ•„์š”ํ•˜๋ฉด
๋‚˜๋ฅผ ๋ˆŒ๋Ÿฌ ๊ตฌ๋ฆฌ๐Ÿป
์ฑ—๋ด‡
๋„ˆ๊ตด
bot
์–ด์„œ์™€ ๊ตฌ๋ฆฌ ส• ยทแดฅยทส”
๋ฌด์—‡์ด ํ•„์š”ํ•˜์…” ๊ตฌ๋ฆฌ?

Trending Tags

๋„์›€์ด ํ•„์š”ํ•˜๋ฉด
๋‚˜๋ฅผ ๋ˆŒ๋Ÿฌ ๊ตฌ๋ฆฌ๐Ÿป
์ฑ—๋ด‡
๋„ˆ๊ตด
bot
์–ด์„œ์™€ ๊ตฌ๋ฆฌ ส• ยทแดฅยทส”
๋ฌด์—‡์ด ํ•„์š”ํ•˜์…” ๊ตฌ๋ฆฌ?