Home » .Net

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

In this article, we will learn about C# .Net searching, its types (Linear/Sequential and Binary) and implementation. Here, you will also find the solved programs on searching in C#.Net.

Searching is the technique to find particular item, here we are discussing some of the popular searching techniques to find an item from the array.

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

Here we will find item 11 which is exist on 3rd (according to array indexing it’s on 2nd) position.

In context of C#, here we will take an array that contains integer elements. Then we will write code to search element from array.

Types of searching

Basically there are two type of searching is used in computer technology:

  1. Internal Searching
  2. External Searching

1) Internal Searching: Searching an item into main memory or primary memory is known as internal searching.

2) External Searching: Searching an item into external or secondary memory is known as external searching.

In arrays or linked list we always perform internal searching. There are some popular methods used in searching are given below:

  1. Linear or sequential search
  2. Binary search

1) Linear or sequential searching implementation in C#

In linear/sequential search, we search given item, sequentially one by one, if we found item, then we return location. It may also possible item is not find till last item of list.

For example we have list: 12 13 10 25 47

Suppose we have an item 25 to search.

  • In first comparison we compare 25 with 12 but her we did not find match.
  • In second comparison we compare 25 with 13 but here we did not find match.
  • In third comparison we compare 25 with 10 but still here we did not find match.
  • In forth comparison we compare 25 with 25. Here we found matching element, then we can say 25 have 4th position in given list.

Consider 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 searching implementation in C#

In this searching, we divide list for searching, here we use 3 variables LOW, HIGH, MID.

LOW - It indicates lower index of list.
HIGH - It indicates highest index of list.
MID - It indicates middle index of list.

For example we have a list in ascending order. 10 20 30 40 50 60 70 80 90 100

And we find item 30 in above list.

Note: Array must be sorted

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.

Consider 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.