Find dichotomy C language

C language binary search: Here Insert Picture Description
condition while's, also the k <0 is written inside the loop to go out of the loop with the break statement, may be used in a break in the loop can also be used in a switch statement. Here Insert Picture Description
C language:

#include
the using namespace STD;
int main ()
{
Short Low {0}, {High}. 19, MID {0}, K {-1};
int m; // you want to find a value
Short I = 0;
int A [] {} 66,88,92,97,101,233,554,655,782,985,1123,2324,2522,2782,3358,45232,88754,69999,78259,232145;
COUT << "array a value:";
for (I = 0; I <20 is; I ++)
COUT << a [I] << "";
COUT << "\ n-enter the value you want to find:";
CIN >> m;

while (low <= high&&k<0)
{
    mid = (low + high) / 2;
    if (m > a[mid])
        low = mid + 1;
    else if (m < a[mid])
        high = mid - 1;
    else
        k = mid;
}
if (k >= 0)
    cout << "index : " << k << endl;
else
    cout << "Not be found."<< endl;
system("pause");

}

Released two original articles · won praise 0 · Views 48

Guess you like

Origin blog.csdn.net/Paul_YQ/article/details/104229063