Search code examples
c#backendbinary-search-tree

How to find correct result using C# binary search? (&print the process)


I want to find an correct result by using C# binary search

here's my code

using System;

namespace BinarySearch
{
    class Program
    {
        public static int BinarySearch(int[] Numbers,int SearchItems)
        {
            int start = 0;
            int end = Numbers.Length - 1;
            while (start<=end)
            {
                int mid = (start + end) / 2;
                if (SearchItems<Numbers[mid])
                {
                    end = mid - 1;
                }
                else
                {
                    start = mid + 1;
                }
            }
            return -1;

        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please Provide input search No. :");
            int SearchItems = Convert.ToInt32(Console.ReadLine());
            int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
            int result = BinarySearch(Numbers, SearchItems);
            if (result<0)
            {
                Console.WriteLine($"{SearchItems} is not found!");

            }
            else
            {
                Console.WriteLine($"{SearchItems} is found at index:{result}");
            }

        }
        
    }
}

when input is 88 ,output is not correct

Please Provide input search No. :
88
88 is not found!

Why is this happening? and I would also like to know how to print out the search process ?

Thank you for the kindness help!


Solution

  • Try :

    using System;
    
    namespace BinarySearch
    {
        class Program
        {
            public static int BinarySearch(int[] Numbers,int SearchItems)
            {
                int start = 0;
                int end = Numbers.Length - 1;
                while (start<=end)
                {
                    int mid = (start + end) / 2;
                    if (SearchItems == Numbers[mid])  //need this code to check equal value
                    {  
                     return ++mid;  
                    }  
                    if (SearchItems<Numbers[mid])
                    {
                        end = mid - 1;
                    }
                    else
                    {
                        start = mid + 1;
                    }
                }
                return -1;
    
            }
            static void Main(string[] args)
            {
                Console.WriteLine("Please Provide input search No. :");
                int SearchItems = Convert.ToInt32(Console.ReadLine());
                int[] Numbers = { 8, 44, 26, 67, 35, 12, 77, 75, 31, 2, 62, 4, 88, 17 };
                Array.Sort(Numbers);
                int result = BinarySearch(Numbers, SearchItems);
                if (result<0)
                {
                    Console.WriteLine($"{SearchItems} is not found!");
    
                }
                else
                {
                    Console.WriteLine($"{SearchItems} is found at index:{result}");
                }
    
            }
            
        }
    }