DEV Community

alokz diptim!
alokz diptim!

Posted on • Edited on

1

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay