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
class 이진탐색
    {
        public const int MAX_SIZE = 10000;
        public static int[] array = new int[MAX_SIZE];
        public static int founded = 0;
 
        //이진 탐색은 정렬이 되어있어야만 사용이 가능하다.
        static int Search(int start, int end, int target)
        {
            if (start > end)
                return -9999;
 
            int mid = (start + end) / 2;
 
            if (array[mid] == target)
            {
                return mid;
            }
            else if (array[mid] > target)
            {
                //중간 인덱스의 값이 찾는 수보다 큰 경우 왼쪽을 찾는다.
                return Search(start, mid - 1, target);
            }
            else
            {
                //중간 인덱스의 값이 찾는 수보다 작은 경우 오른쪽을 찾는다.
                return Search(mid + 1, end, target);
            }
        }
    }
cs

'공부 > 알고리즘 및 기타 공부' 카테고리의 다른 글

투 포인터 알고리즘 (C#)  (0) 2023.04.24
우선순위 큐 (C#)  (0) 2023.04.22
이진트리 구현 (C#)  (0) 2022.02.03
퀵 정렬 (C#)  (0) 2022.01.27
삽입 정렬 (C#)  (0) 2022.01.26

+ Recent posts