?? 折半查找.cpp
字號:
#include<stdio.h>
#define N 15
void main()
{
void bi_search( int[],int ,int );
int a[100],x,i,n=15;
printf("請輸入數(shù)據(jù):\n");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
printf("輸入要查找的數(shù)據(jù)x:\n");
scanf("%d",&x);
bi_search(a,n,x);
}
void bi_search(int a[],int n, int x)
{ int mid, top, bot, find;
top = 0; bot = n-1; find = 0;
do
{ mid =( top + bot ) / 2;
if( x == a[mid] )
{ printf("找到了%3d,是 a[%d]",x,mid);
printf("\n");
find = 1; }
else if( x < a[mid] ) bot = mid - 1;
else if( x > a[mid] ) top = mid + 1;
} while( top <= bot && find == 0 );
if( find == 0 )
printf("%3d 沒有找到",x);
printf("\n"); }
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -