二分查找

  • C语言二分查找代码怎么写

    C语言二分查找代码如下:,,``c,#include ,,int binary_search(int arr[], int size, int target) {, int left = 0;, int right = size - 1;,, while (left ˂= right) {, int mid = left + (right - left) / 2;,, if (arr[mid] == target) {, return mid;, } else if (arr[mid] ˂ target) {, left = mid + 1;, } else {, right = mid - 1;, }, },, return -1;,},,int main() {, int arr[] = {1, 3, 5, 7, 9};, int size = sizeof(arr) / sizeof(arr[0]);, int target = 5;,, int result = binary_search(arr, size, target);, printf("元素 %d 在数组中的位置是: %d,", target, result);,, return 0;,},``

    2024-01-03
    0175
免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入