Searching in C#.net, types of searching and implementations using C# program

In this article, we will find out about C# .Net looking, its sorts (Linear/Sequential and Binary) and usage. Here, you will likewise locate the understood projects on looking in C#.Net.

Looking is the method to discover a specific thing, here we are talking about a portion of the mainstream looking through procedures to discover a thing from the cluster.

For example, we have a rundown: 12 15 11 8 34 48

Here we will discover thing 11 which is existing on third (as per exhibit ordering it’s on second) position.

In the setting of C#, here we will take a cluster that contains number of components. At that point, we will compose code to look through component from the cluster.

Kinds of looking

Fundamentally there are two sorts of looking is utilized in PC innovation:

  1. Internal Searching
  2. External Searching

1) Internal Searching: Searching a thing into fundamental memory or essential memory is known as internal looking.

2) External Searching: Searching a thing into external or auxiliary memory is known as external looking.

In clusters or connected rundown, we generally perform internal looking. There are some mainstream techniques utilized in looking are given beneath:

Direct or consecutive search

Binary inquiry

1) Linear or consecutive looking through execution in C#

In straight/successive pursuit, we search given thing, consecutively individually, in the event that we discovered the thing, at that point we return area. It might likewise conceivable thing isn’t to discover till the last thing of the rundown.

For example, we have list: 12 13 10 25 47

Assume we have a thing 25 to look.

  1. In the first examination, we contrast 25 and 12 yet her we didn’t discover coordinate.
  2. In the second examination, we contrast 25 and 13 yet here we didn’t discover coordinate.
  3. In third correlation we contrast 25 and 10 yet at the same time here we didn’t discover coordinate.
  4. In forward examination, we contrast 25 and 25. Here we discovered coordinating component, at that point we can say 25 have the fourth position in the given rundown.

Think about the program:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i    = 0 ;
            int item = 0 ;
            int pos  = 0 ;

            int[] arr = new int[5];
            
            //Read numbers into array
            Console.WriteLine("Enter elements : ");
            for (i = 0; i < arr.Length; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr[i] = int.Parse(Console.ReadLine());
            }

            Console.Write("Enter item to search : ");
            item = int.Parse(Console.ReadLine());

            //Loop to search element in array.
            for (i = 0; i < arr.Length; i++)
            {
                if (item == arr[i])
                {
                    pos = i + 1;
                    break;
                }
            } 

            if (pos == 0)
            {
                Console.WriteLine("Item Not found in array");
            }
            else 
            {
                Console.WriteLine("Position of item in array: "+pos);
            }
            
        }
    }
}

Output:

Enter elements :
Element[1]: 25
Element[2]: 12
Element[3]: 13
Element[4]: 16
Element[5]: 29
Enter item to search : 16
Position of item in array: 4

Binary looking through execution in C#

In this looking, we separate rundown for looking, here we utilize 3 factors LOW, HIGH, MID.

LOW – It demonstrates lower file of the rundown.

HIGH – It shows most noteworthy file of a rundown.

MID – It demonstrates the center list of a rundown.

For example, we have a rundown in the climbing request. 10 20 30 40 50 60 70 80 90 100

Furthermore, we discover thing 30 in the above rundown.

Note: Array must be arranged

1st comparison: 
	LOW	= 1
	HIGH	=  10
	MID	=  (1+10) /2 = 5

Now if LIST[MID] == ITEM, (50 == 30) No Match.

Then find new indices by dividing list. Here ITEM is less than mid value then:
LOW = 1
HIGH = MID -1 =4
MID =  (1+4)/2 = 2 
  
2nd comparison:
IF LIST[MID] == ITEM, (20==30) No Match.

Then find new indices by dividing list. Here ITEM is greater than mid value then:
LOW =  MID + 1 = 3
HIGH = 4
MID =  (3+4)/2 = 3


3rd  comparison:
IF LIST[MID] == ITEM, (30==30), No ITEM found at position 3.

Think about the program:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            int i    = 0 ;
            int item = 0 ;
            int pos  = 0 ;

            int[] arr = new int[5];

            int LOW  = 0;
            int HIGH = 0;
            int MID  = 0;

            //Read numbers into array
            Console.WriteLine("Enter elements : ");
            for (i = 0; i < arr.Length; i++)
            {
                Console.Write("Element[" + (i + 1) + "]: ");
                arr[i] = int.Parse(Console.ReadLine());
            }

            Console.Write("Enter item to search : ");
            item = int.Parse(Console.ReadLine());


            HIGH = arr.Length - 1;
            MID = (LOW + HIGH) / 2;

            //Loop to search element in array.
            while (LOW <= HIGH)
            {
                if (item < arr[MID])
                {
                    HIGH = MID - 1;
                    MID = (LOW + HIGH) / 2;
                }
                else if (item > arr[MID])
                {
                    LOW = MID + 1;
                    MID = (LOW + HIGH) / 2;
                }
                else if (item == arr[MID])
                {
                    pos = MID + 1;
                    break;
                }
            }

            if (pos == 0)
            {
                Console.WriteLine("Item Not found in array");
            }
            else 
            {
                Console.WriteLine("Position of item in array: "+pos);
            }
            
        }
    }
}

Output:

Enter elements :
Element[1]: 10
Element[2]: 20
Element[3]: 30
Element[4]: 40
Element[5]: 50
Enter item to search : 40
Position of item in array: 4

Leave a Comment

error: Alert: Content is protected!!