求C语言编写程序折半查找程序
发布网友
发布时间:2022-05-01 15:39
我来回答
共2个回答
热心网友
时间:2022-06-18 22:24
BubbleSort(A)
{
for i=1 to n
for j=n to i+1
if A[j]<A[j-1]
swap A[j]<->A[j-1]
}
热心网友
时间:2022-06-18 22:25
#include<stdio.h>
#define N 20
int Binary(int a[], int n, int found)
{
int low = 0;
int high = n - 1;
int mid;
do
{
mid = (low + high) / 2;
if(found == a[mid])
return mid;
else if(found < a[mid])
high = mid - 1;
else
low = mid + 1;
}while(low <= high);
return -1;
}
void main()
{
int found,a[N] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
printf("please input a number\n");
scanf("%d",&found);
if(Binary(a,N, found))
printf("在数组中的位置是 %d\n",Binary(a,N, found));
else
printf("没有找到\n");
}