# 数组中的二进制搜索

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

srand(time(NULL));
int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
int randomIndex = rand() % 20;
int randomValue = array[randomIndex];
int low = 0;
int high = 20;
int middle = (low + high) / 2;

printf("The random number to find is %d\n", randomValue);

while (middle <= randomValue) {

if (middle < randomValue) {
printf("The number %d is lower than the random number\n", middle);

}

if (middle > randomValue) {
printf("The number %d is lower than the random number\n", middle);
}

if (middle == randomValue) {
printf("The number %d  is the correct random number", middle);
}

}
return 0;

}
``````

``````The number 10 is lower than the random number

The number 15 is higher than the random number

The number 12 is lower than the random number

The number 13 is the correct random number
``````

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {

srand(time(NULL));
int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
int randomIndex = rand() % 19;
int randomValue = array[randomIndex];
int low = 0;
int high = 19;
int middle = (low + high) / 2;

printf("The random number to fine is %d\n", randomValue);

while (middle <= randomValue) {

if (middle < randomValue) {
printf("The number %d is lower than the random number\n", middle);
low  = ;
high = ;
middle = (low + high) / 2;
}

if (middle > randomValue) {
printf("The number %d is lower than the random number\n", middle);
low  = ;
high = ;
middle = (low + high) / 2;

}

if (middle == randomValue) {
printf("The number %d  is the correct random number", middle);
}

}
return 0;

}
``````