Skip to content
Related Articles

Related Articles

C# – Perform Searching using Predefined Functions

Improve Article
Save Article
  • Last Updated : 28 May, 2022
Improve Article
Save Article

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. 


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.


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

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


  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.



// 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
    // 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,
    // Display the position of the slement
    Console.WriteLine("Element {1} is present in  {0} position",
                      data3, array1[data3]);


Enter the size of array 
Enter data :
Search a number : 
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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!