본문 바로가기

스파르타코딩클럽(내일배움캠프)

스파르타 코딩 클럽 4주차 3일

728x90

[알고리즘 강의]

이진 탐색


public class Main
{
    static int result =0;
    static int []finding_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
    static int first = 0;
    static int last = finding_numbers.length-1;
    public static void is_existing_target_number_binary(int target,int cur)
    {

        int mid = (int)(last + first)/2;
        cur = mid;

        if(target==finding_numbers[mid])
        {
            System.out.printf("result : [" + cur +" 번쨰] -> " + finding_numbers[cur]);
            return;
        }
        else if(target>finding_numbers[mid])
        {
            mid +=1;
            first= mid;
            is_existing_target_number_binary(target,mid);
        }
        else if(target<finding_numbers[mid])
        {
            mid-=1;
            last= mid;
            is_existing_target_number_binary(target,mid);
        }
        else
        {
            System.out.printf("no search value");
        }
        return;
    }
    public static void main(String[] args)
    {
        is_existing_target_number_binary(4,0);
        return;
    }
}
728x90