Forem

alokz diptim!
alokz diptim!

Posted on

2 1

Binary search using c#

alt text

I'll be breaking down the documentation if you need help.

    public static int BinarySearch(int arraySize, int targetNumber)
    {

        Random random = new Random();
        int[] figures = new int[arraySize];

        while (!figures.Contains(targetNumber))
        {
            for (int i = 0; i < figures.Length; i++)
            {
                figures[i] = random.Next(0, 15);
            }
        }

        //SORTING OUT THE ARRAY
        Array.Sort(figures);

        int minimum = 0;
        int maximum = figures.Length - 1;

        l1:

        int center = (maximum + minimum) / 2;

        if (figures[center] == targetNumber)
        {
            return center;
        }

        if (minimum > maximum)
        {
            Console.WriteLine("This kind of Array doesnt exist and this is false. Please add some element to the element. Thanks!");
        }

        if (figures[center] < targetNumber)
        {
            minimum = center + 1;
        }

        if (figures[center] > targetNumber)
        {
            maximum = center - 1;
        }

        if (figures[center] == targetNumber)
        {
            return center;
        }

        goto l1;

    }

    static void Main(string[] args)
    {
        Console.WriteLine(BinarySearch(10, 8));

        Console.WriteLine("The End");
        Console.Read();
    }
}

}

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay