学无先后,达者为师

网站首页 编程语言 正文

C#二分查找算法_C#教程

作者:農碼一生   更新时间: 2022-06-28 编程语言

1、定义:

折半搜索,也称二分查找算法、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。

要计算把目标值插入到该数组中的索引值。最开始的思路:

①.先把目标数插入到数组中

②.进行排序

③.返回索引

2、实现代码:

  public static int process4(int[] arr, int low, int high, int key)
  {
    int mid = (low + high) / 2;
    if (low > high)
      return -1;
    else
    {
      if (arr[mid] == key)
        return mid;
      else if (arr[mid] > key)
        return process4(arr, low, mid - 1, key);
      else
        return process4(arr, mid + 1, high, key);
    }
  }

原文链接:https://www.cnblogs.com/wml-it/p/16048142.html

栏目分类
最近更新