DEV Community

alokz diptim!
alokz diptim!

Posted on • Updated on

Linear Search with c#

 class Program
    {

        static void Main(string[] args)
        {
            linearIndex(0);
            Console.ReadLine();
        }

        public static int linearIndex(int searchNumber)
        {

            int index = 0;
            int[] intArray = { 
        47,56,38,5,40,17,26,58,56,25,10,14,38,10,36,15,36,53,55,33,10,48,6,45,

       38,10,49,47,53,10,52,45,32,46,55,19,55,53,11,39,36,25,26,41,15,1,43,
       40,13,53,39,11,53,33,5,21,36,53,15,36};

         l1:   if (searchNumber == intArray[index])
            {
                Console.WriteLine("found at! : {0}", index);
                return index;
            }

            index ++;
            if (index < intArray.Length)
            {
                goto l1;
            }
            else
            {
                Console.WriteLine("Not found at any Index");
            }

            return 0;
        }

    }

The console app started from the main method and I passed in an int 0 to check against the arrayList,

Well, there is a comparison and as long as the index of the array is less than the array length, then we are good to continue the search.

If the option is maxed out then that means we didn't get to see the number.

Oldest comments (0)