#include <stdio.h>
void My_sort(int arr[],int sz)
{
int i = 0;
int j = 0;
for (i = 0; i < sz - 1; i++)
{
for (j = 0; j < sz - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int t = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = t;
}
}
}
}
int My_lin(int arr[], int sz)
{
int r = 0;
int left = 0;
int right = sz-1;
printf(" \n");
printf("请输入想要查找的数:");
scanf("%d", &r);
while(left<right)
{
int mid = left + (right - left)/2;
if (arr[mid] < r)
{
left = mid + 1;
}
else if (arr[mid] > r)
{
right = mid - 1;
}
else
{
printf("查找成功:");
return mid;
}
}
if (left > right)
{
printf("查找失败,请重新输入:");
return 0;
}
}
int main()
{
int i = 0;
int arr[] = { 1,3,5,7,9,2,4,6,8,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
My_sort(arr, sz);
while(1)
{
int ret = My_lin(arr, sz);
printf("下标=%d\n", ret);
}
return 0;
}