Searching in C#.Net, Types of Searching, Their Implementations

In this tutorial, 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. By IncludeHelp Last updated : April 15, 2023

Overview

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:
    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.

Popular Searching Algorithms

There are mainly two popular searching algorithms. They are:

  1. Linear or Sequential Search
  2. Binary Search

1. Linear or Sequential Search

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.

Example

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.

C# Implementation of Linear or Sequential Search

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

2. Binary Search

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

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

Example

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.

C# Implementation of Binary Search

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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.