Home >Backend Development >C#.Net Tutorial >How to write a binary search algorithm using C#

How to write a binary search algorithm using C#

WBOY
WBOYOriginal
2023-09-19 12:42:371284browse

How to write a binary search algorithm using C#

How to use C# to write a binary search algorithm

The binary search algorithm is an efficient search algorithm. It searches for the position of a specific element in an ordered array, and the time is complex. The degree is O(logN). In C#, we can write the binary search algorithm through the following steps.

Step 1: Prepare data

First, we need to prepare a sorted array as the target data for the search. Suppose we want to find the position of a specific element in an array.

int[] data = {1, 3, 5, 7, 9, 11, 13, 15};

Step 2: Write a binary search function

Next, we can write a function to implement the binary search algorithm. The parameters of the function include the element value to be found and the array to be found. The return value of the function is the index value of the found element in the array, or -1 if it is not found.

int BinarySearch(int target, int[] data)
{
    int left = 0;
    int right = data.Length - 1;

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

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

Step 3: Call the binary search function

Now, we can call the binary search function written above in the main program to find specific elements. We can first define an element value to be searched, then call the binary search function and print out the result.

int target = 13;
int result = BinarySearch(target, data);

if (result == -1)
{
    Console.WriteLine("在数组中未找到该元素!");
}
else
{
    Console.WriteLine("该元素在数组中的索引为:" + result);
}

The complete code example is as follows:

using System;

public class BinarySearchExample
{
    static void Main(string[] args)
    {
        int[] data = {1, 3, 5, 7, 9, 11, 13, 15};
        int target = 13;

        int result = BinarySearch(target, data);

        if (result == -1)
        {
            Console.WriteLine("在数组中未找到该元素!");
        }
        else
        {
            Console.WriteLine("该元素在数组中的索引为:" + result);
        }
    }

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

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

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

        return -1;
    }
}

Through the above code example, we can learn how to use C# to write a binary search algorithm. In actual development, we can adjust and optimize the algorithm according to specific needs to achieve a more efficient search function.

The above is the detailed content of How to write a binary search algorithm using C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn