3楼?要排序咩?直接找都比你快啦!哈哈......
你到底懂不懂,不懂不要乱发言,用折半查找你写个程序,我输入数据,你给我找出来我看看
#include <stdio.h>
#define N 10
int binary_search(int a[], int key, int left, int right);//二分查找(折半查找)
void bubble_sort(int a[]);//从小到大排序
int main(void)
{
int i, key, a[] = {74, 5, 45, 2, 21, 6, 7, 8, 15, 10};
printf("Enter search number:\n");
scanf("%d", &key);
bubble_sort(a);
i = binary_search(a, key, 0, N - 1);
printf("i = %d\n", i);
return 0;
}
int binary_search(int a[], int key, int left, int right)
{
int mid;
mid = (left + right) / 2;
if (left > right)
return -1;
else if (left <= right)
{
if (a[mid] == key)
return mid;
else if (a[mid] > key)
binary_search(a, key, left, mid - 1);
else if (a[mid] < key)
binary_search(a, key, mid + 1, right);
}
}
void bubble_sort(int a[])
{
int i, j, temp;
for (i = 0; i < N - 1; i++)
for ( j = 0; j < N - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}