#include "..\CookHeader.h"

Array <int> dataAry ;
int loopCount = 0;

int binSearch(Array <int> ary, int fData) {
    int pos = -1;
    int start = 0;
    int end = len(ary) - 1;

    while (start <= end) {
        loopCount++;
        int mid = (start + end) / 2;
        if (fData == ary[mid])
            return mid;
        else if (fData > ary[mid])
            start = mid + 1;
        else
            end = mid - 1;
    }
    return pos;
}

int main() {
    randomInit(0, 100000);

    for (int r = 0; r < 100000; r++) {
        int num = cookRandom(gen);
        dataAry.push_back(num);
    }
    sortArray(dataAry);
    int findData = cookRandom(gen);

    print("배열 일부-->  ");
    // 일부만 출력
    Array <int> frontAry = { dataAry.begin(), dataAry.begin() + 5 };
    Array <int> rearAry = { dataAry.end() - 5, dataAry.end() };
    printArray_(frontAry);
    print(" ...");
    printArray_(rearAry);
    println("");

    int position = binSearch(dataAry, findData);
    if (position == -1) {
        println(to_string(findData) + "이(가) 없네요.");
    }
    else {
        println(to_string(findData) + "은(는) " + to_string(position) + " 위치에 있음");
    }
    println("##" + to_string(loopCount) + "회 검색함");
}
