RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
C++ 查找算法
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 10:40
  • 来源:未知

int SeqSearch(int arr[], int len, int target) { ??? int i = 0; ??? while ((i != len) && (arr[i] != target)) ??? { ??????? i++; ??? } ??? if (i < len) ??? { ??????? return i; ??? } ??? else ??? { ??????? return -1; ??? } }

int BinSearch(int arr[], int len, int target){??? int low = 0;??? int high = len-1;??? while (low <= high)??? {??????? int mid = (low + high) / 2;??????? if (arr[mid] == target)??????? {??????????? return mid;??????? }??????? else if (arr[mid] > target)??????? {??????????? high = mid - 1;??????? }??????? else??????? {??????????? low = mid + 1;??????? }??? }??? return -1;}