Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
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
Tags
more
Archives
Today
Total
관리 메뉴

개발자이야기

이분탐색 java 본문

알고리즘

이분탐색 java

개발자가되고싶어 2020. 7. 21. 10:47
public static void main(String[] args) {

        long[] room = {1,2,3,4,5};
        int find = 4; //내가 찾을값

        int pivot = 0;
        int left = 0;
        int right = room.length-1;
        while (room[pivot] != find) {
            pivot = (left+right) / 2;
            if(find > room[pivot]) {
                left = pivot+1;
            } else if(find < room[pivot]) {
                right = pivot-1;
            } else {
                //같은경우
                System.out.println(room[pivot]);
            }
        }
    }

'알고리즘' 카테고리의 다른 글

프로그래머스 가장 먼 노드  (0) 2021.09.05
프로그래머스 입국심사  (0) 2021.09.03
카카오 알고리즘 테스트 호텔방 배정  (0) 2020.07.20