#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int list[100];
int i, j;
for(i=0;i<100;i++){
list[i]=rand()%100;
}
int n;
for(j=0; j<99; j++){
for(i=j+1; i<100;i++){
if(list[j]>list[i]){
n=list[j];
list[j]=list[i];
list[i]=n;
}
}
}
for(i=0;i<100;i++){
printf("%d %d\n", i, list[i]);
}
printf("\n----------------\n");
n=50;
scanf("%d", &j);
int min=0, max=99;
while(1){
if((n==0) || (n>=98) || (list[n]==j) || (list[n]<j && list[n+1] >j)){
printf("%d %d", n, list[n]);
break;
}else if(list[n]>j){
max=n+1;
}else{
min=n-1;
}
n=(int)((min+max)/2);
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: