#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int arr[10] = { 0 };
int i = 0;
int j = 0;
int t = 0;
int right, left, mid;
int w = 0;
srand(time(0));
for (i = 0; i < 10; i++)
{
arr[i] = rand() % 90 + 10;
}
for (i = 0; i < 10; i++)
{
printf("%5d", arr[i]);
}
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9 - i; j++)
{
if (arr[j] > arr[j + 1])
{
t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
printf("\n");
for (i = 0; i < 10; i++)
{
printf("%5d", arr[i]);
}
printf("\n");
printf("-------------------------------\n");
printf("请输入");
scanf("%d", &w);
right = sizeof(arr) / sizeof(arr[0]) - 1;
left = 0;
while (left <= right)
{
mid = (right + left) / 2;
if (w < arr[mid])
{
right = mid - 1;
}
else if (w > arr[mid])
{
left = mid + 1;
}
else
{
printf("%d", mid);
break;
}
}
if (left > right)
printf("error");
return 0;
}
冒泡法+二分查找法
于 2024-04-15 10:50:14 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)