二分法查找

unsigned int BinarySearch(int array[],unsigned int len, int find_data)
{
	unsigned int i = 0,start = 0, mid = 0, end = len - 1;

	if ((NULL == array) || (len < 1))
	{
                printf("input error!!!");
		return -1;
	}
        for(i = 0;i < len;i++)
        {
                 f(find_data == array[i])
                 {
                        break;
                 }
         }
          if(i == len)
          {
                  printf("the data is not in the array.error!!!");
                   return -1;
            }

	while (start <= end)
	{
		mid = (start + end) / 2;

		if (find_data > array[mid])
		{
			start = mid + 1;
		}
		else if (find_data < array[mid])
		{
			end = mid - 1;
		}
		else
		{
			return mid;
		}
	}
}

 
  

你可能感兴趣的:(C语言)