1
Answer

Binary Search coding

Philip Harris

Philip Harris

9y
710
1
So the last couple weeks ive been working on Linear and Jump searches but now I am doing Binary and would like some one to take a look at my code I know I have made a mistake somewhere in it






Here is what im trying to code in c# (algorithm 2.4)





here is where i am at


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace binary
{
   
        class Program
        {
            static void Main()
            {
                var list = new List<int> { 3, 5, 7, 8, 10 }; 
                int lower = 1;
                int upper = list.Count;
                int middle = (int)Math.Sqrt(upper);
                int[] binary = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                foreach (int X in binary)
                {
                   


                    if (lower == upper)
                    {
                        if (X ==list[lower])
                        {
                        Console.WriteLine("{0} was found at index {1}", X, lower);
                        }
                        else { Console.WriteLine("0"); }
                    }
                    else
                    {
                        middle = (lower + upper) / 2;
                        if (X > list[middle])
                        {
                            Console.WriteLine("list {0} mid {1} upper {2} and x{3}",list,middle+1,upper, X);
                        }
                        else { Console.WriteLine("list {0} mid {1} upper {2} and x{3}",list,middle+1,upper, X);}
                    }
                }


                Console.ReadKey(); 
            }
        }
}

Answers (1)