C# 二分查找

        二分查找(Binary Search)是一种在有序数组或列表中查找特定元素的搜索算法。该算法比较要搜索的值和数组的中间元素。如果要搜索的值小于中间元素,则在数组的左半部分继续搜索;如果要搜索的值大于中间元素,则在数组的右半部分继续搜索。不断重复这个过程,直到找到要搜索的值或确定搜索范围为空为止。

        二分查找是一种高效的查找算法,因为在每次比较后,搜索范围可以减半,时间复杂度为O(log n)。二分查找的前提是数组或列表中元素已按照某种顺序(通常是升序)排列。

具体步骤如下:

  1. 初始化左指针left和右指针right,分别指向数组的起始和结束位置。
  2. 不断计算中间位置mid,即mid = (left + right) / 2。
  3. 比较要搜索的值与数组中间元素array[mid]的大小。
  4. 如果要搜索的值等于array[mid],则找到目标,返回mid。
  5. 如果要搜索的值小于array[mid],则更新右指针right = mid - 1。
  6. 如果要搜索的值大于array[mid],则更新左指针left = mid + 1。
  7. 重复上述过程,直到找到目标或确定搜索范围为空。

如果数组或列表不是有序的,可以先对其进行排序,然后再利用二分查找进行搜索。

C#中实现二分查找的完整示例代码:

using System;

class BinarySearchExample
{
    static int BinarySearch(int[] array, int target)
    {
        int left = 0;
        int right = array.Length - 1;

        while (left <= right)
        {
            int mid = left + (right - left) / 2;

            if (array[mid] == target)
            {
                return mid;
            }
            else if (array[mid] < target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }

        return -1; // Target not found in the array
    }

    static void Main()
    {
        int[] array = { 1, 3, 5, 7, 9, 11, 13, 15, 17 };
        int target = 9;

        int result = BinarySearch(array, target);

        if (result != -1)
        {
            Console.WriteLine($"Target {target} found at index {result}");
        }
        else
        {
            Console.WriteLine("Target not found in the array");
        }
    }
}

这个示例代码实现了一个对有序数组进行二分查找的功能。你可以根据需要修改数组和目标值以进行测试。

示例二 查找字符串:

        在C#中,二分查找通常是用于在有序数组中查找数值类型的数据,而不是用于在字符串数组中查找字符串。不过,如果你需要在有序的字符串数组中查找特定的字符串,你可以按照以下步骤创建一个二分查找的示例代码:

using System;

class StringBinarySearchExample
{
    static int StringBinarySearch(string[] array, string target)
    {
        int left = 0;
        int right = array.Length - 1;

        while (left <= right)
        {
            int mid = left + (right - left) / 2;

            int compareResult = String.Compare(array[mid], target);

            if (compareResult == 0)
            {
                return mid;
            }
            else if (compareResult < 0)
            {
                left = mid + 1;
            }
            else
            {
                right = mid - 1;
            }
        }

        return -1; // Target not found in the array
    }

    static void Main()
    {
        string[] array = { "apple", "banana", "orange", "strawberry", "watermelon" };
        string target = "orange";

        int result = StringBinarySearch(array, target);

        if (result != -1)
        {
            Console.WriteLine($"Target {target} found at index {result}");
        }
        else
        {
            Console.WriteLine("Target not found in the array");
        }
    }
}

        在上面示例代码中,修改了二分查找算法,使其可以在有序的字符串数组中查找特定的字符串。你可以根据需要修改字符串数组和目标字符串以进行测试。

相关推荐

  1. c++】二分查找教程

    2024-02-19 11:56:01       64 阅读
  2. C++二分查找

    2024-02-19 11:56:01       57 阅读
  3. C语言-二分查找

    2024-02-19 11:56:01       46 阅读
  4. C# 二分查找

    2024-02-19 11:56:01       45 阅读
  5. [C语言]二分查找

    2024-02-19 11:56:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 11:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 11:56:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 11:56:01       82 阅读
  4. Python语言-面向对象

    2024-02-19 11:56:01       91 阅读

热门阅读

  1. C语言:生成校验码

    2024-02-19 11:56:01       50 阅读
  2. 【SpringBoot5】SpringBoot如何找到我们写的类的

    2024-02-19 11:56:01       44 阅读
  3. android pdf框架-3,基于recyclerview修改

    2024-02-19 11:56:01       63 阅读
  4. 关于Http和Https

    2024-02-19 11:56:01       51 阅读
  5. CF1781 D. Many Perfect Squares [数学题]

    2024-02-19 11:56:01       51 阅读
  6. OpenCV中saturate_cast模板函数

    2024-02-19 11:56:01       50 阅读
  7. 1.函数模板基础

    2024-02-19 11:56:01       49 阅读