⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarysearch.c

📁 这个是简单的初级的算法代码汗 不要在冒了
💻 C
字号:
#include <stdio.h>

int binarySearch(int list[],int end,int target,int *locn)
{
  int first,mid,last;
  int found;

  first = 0;
  last = end;
  while (first <= last)
  {
    mid = (first + last)/2;
    if (target > list[mid])
        first = mid + 1;
    else if (target < list[mid])
            last = mid -1;
    else
        first = last + 1;
  }
  *locn = mid;

  if (target == list[mid])
    found = 1;
  else
    found = 0;

  return found;
}

void main()
{
   int list1[10]={2,5,9,12,14,21,22,32,34,45},i;
   int location;

   for(i=0;i<10;i++)
      printf("%d  ",list1[i]);
   printf("\n");

   if (binarySearch(list1,9,22,&location) == 1)
      printf("target is 21,the loacation is : %d",location);

   getch();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -