Sai A Sai A
Updated date Jun 19, 2023
In this blog, we delve into the world of array searching in C#. We cover various methods to search for an element in an array, including the popular linear search and binary search algorithms.
  • 1.3k
  • 0
  • 0

Introduction:

Searching for an element in an array is a fundamental operation in programming. Whether you are a beginner or an experienced developer, understanding different search algorithms and their implementation can greatly enhance your problem-solving skills. In this blog, we will explore multiple methods to search for an element in an array using C#, providing code examples and explanations for each approach.

Method 1: Linear Search

Linear search is a simple and straightforward algorithm that sequentially checks each element in the array until a match is found. Here's an example program that demonstrates linear search:

using System;

public class Program
{
    public static int LinearSearch(int[] array, int target)
    {
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] == target)
                return i;
        }
        return -1;
    }

    public static void Main()
    {
        int[] array = { 5, 2, 9, 1, 7 };
        int target = 9;
        int index = LinearSearch(array, target);

        if (index != -1)
            Console.WriteLine($"Element {target} found at index {index}.");
        else
            Console.WriteLine($"Element {target} not found in the array.");
    }
}

Output:

Element 9 found at index 2.

The LinearSearch method iterates through each element in the array and compares it with the target value. If a match is found, it returns the index where the element was found. If no match is found, it returns -1.

Method 2: Binary Search

Binary search is an efficient algorithm for searching in a sorted array. It follows a divide-and-conquer approach by repeatedly dividing the search space in half. Here's an example program that demonstrates binary search:

using System;

public class Program
{
    public static int BinarySearch(int[] array, int target)
    {
        int left = 0;
        int right = array.Length - 1;

        while (left <= right)
        {
            int mid = left + (right - left) / 2;

            if (array[mid] == target)
                return mid;

            if (array[mid] < target)
                left = mid + 1;
            else
                right = mid - 1;
        }

        return -1;
    }

    public static void Main()
    {
        int[] array = { 1, 2, 5, 7, 9 };
        int target = 5;
        int index = BinarySearch(array, target);

        if (index != -1)
            Console.WriteLine($"Element {target} found at index {index}.");
        else
            Console.WriteLine($"Element {target} not found in the array.");
    }
}

Output:

Element 5 found at index 2.

The BinarySearch method works by maintaining a left and right boundary in the sorted array. It calculates the middle index and compares the middle element with the target. If they are equal, the element is found. If the middle element is smaller than the target, the left boundary is moved to mid + 1. Otherwise, the right boundary is moved to mid - 1. This process continues until the target element is found or the boundaries overlap, indicating that the element is not present in the array.

Conclusion:

In this blog, we explored two commonly used search algorithms, linear search and binary search, for finding an element in an array using C#. Linear search is suitable for small arrays or unsorted arrays, while binary search excels for large sorted arrays. Understanding these algorithms and their implementation will empower you to choose the most efficient approach based on the specific requirements of your program.

Comments (0)

There are no comments. Be the first to comment!!!