Open In App

C# – Perform Searching using Predefined Functions

Last Updated : 28 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, now our task is to perform searching an element in an array using predefined functions in C#. So, the best searching technique is Binary search and will search the given element in an array using predefined functions. Binary search is an efficient algorithm that will work only on sorted sets of elements. So if we want to use binary search, then the array must be sorted, otherwise, it will give wrong results. So to sort the array we use the Sort() function. Binary search will search in a sorted array by repeatedly dividing the search interval in half. It will begin with an interval covering the whole array. If the element of the search is less than the item in the middle of the interval, narrow the interval to the lower half. Else narrow it to the upper half. It will check repeatedly until the element is found or the interval is empty. We can perform a binary search using BinarySearch() function. 

Syntax:

Array.BinarySearch(array_name, (Object)element)

where array_name is the input array and element is the element to be searched with the Object data type.

Example:

Input:
Array size: 5
Elements: {3, 4, 5, 2, 1}
Element to Search: 4
Output:
Present in 3rd position

Input:
Array size: 1
Elements: {1}
Element to Search: 1
Output:
Present in 0 th position

Approach:

  1. Read the array size from the user.
  2. Read the elements into an array from the user.
  3. Sort the array using Sort() function.
  4. Read the element to be searched in the given array from the user.
  5. Get the element in an array using Binary search function.
  6. Display the specified element along with its position in the array.

Example:

C#




// C# program to search elements in the
// array using predefined functions
using System;
 
class GFG{
     
public static void Main()
{
    Console.WriteLine("Enter the size of array ");
     
    // Read the array size
    string n = Console.ReadLine();
    int data = Int32.Parse(n);
 
    // Declare an array
    int[] array1 = new int[data];
    Console.WriteLine("Enter data :");
    for(int i = 0; i < data; i++)
    {
        string s = Console.ReadLine();
        array1[i] = Int32.Parse(s);
    }
 
    // Sort an array by using Sort() function
    Array.Sort(array1);
 
    // Read a number to search an array
    Console.WriteLine("Search a number : ");
    string search = Console.ReadLine();
    int data2 = Int32.Parse(search);
     
    // Apply BinarySearch() function to
    // search the specified element
    int data3 = Array.BinarySearch(array1,
                                   (Object)data2);
                                    
    // Display the position of the slement
    Console.WriteLine("Element {1} is present in  {0} position",
                      data3, array1[data3]);
}
}


Output:

Enter the size of array 
5
Enter data :
1
4
2
7
8
Search a number : 
2
Element 2 is present in  1 position

Time Complexity: O(N logN), where N represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads