1.折半查找算法
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int left = 0;
int right = sizeof(arr)/sizeof(arr[0])-1;
int key = 7;
int mid = 0;
while(left<=right)
{
mid = (left+right)/2;
if(arr[mid]>key)
{
right = mid-1;
}
else if(arr[mid] < key)
{
left = mid+1;
}
else
break;
}
if(left <= right)
printf("找到了,下标是%d\n", mid);
else
printf("找不到\n");
}
如果实现一个二分查找函数:
int bin_search(int arr[], int left, int right, int key)
{
int mid = 0;
while(left<=right)
{
mid = (left+right)/2;
if(arr[mid]>key)
{
right = mid-1;
}
else if(arr[mid] < key)
{
left = mid+1;
}
else
return mid;//找到了,返回下标
}
return -1;//找不到
}
2.编写代码,演示多个字符从两端移动,向中间汇聚
#include <stdio.h>
int main()
{
char arr1[] = "we are Chinese...";
char arr2[] = "##############";
int left = 0;
int right = strlen(arr1)-1;
printf("%s\n", arr2);
while(left<=right)
{
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
left++;
right--;
}
return 0;
}