Binary Search Method (Static Method In Array Class)
Syntax for Binary Search Method is : - BinarySearch(sortedarray,valuetosearch)
class BinarySearchMethod
{
static void Main()
{
int[] arr = {14,24,34,44,54,64,74,84,94,104}; //Array must be in sorted order (ascending).
int numbertofind = 34;
int index = Array.BinarySearch(arr,numbertofind); //It return index of numbertofind.
Console.WriteLine(numbertofind + " is at "+ index +" index");
Console.WriteLine("Its position is " + (index+1));
}
}
---------------------------------------------------------------------------------------------------------
It is used to search a particular value in a given array.
-----------------------------------------------------------------------------------------------------------------------------
using System;class BinarySearchMethod
{
static void Main()
{
int[] arr = {14,24,34,44,54,64,74,84,94,104}; //Array must be in sorted order (ascending).
int numbertofind = 34;
int index = Array.BinarySearch(arr,numbertofind); //It return index of numbertofind.
Console.WriteLine(numbertofind + " is at "+ index +" index");
Console.WriteLine("Its position is " + (index+1));
}
}
---------------------------------------------------------------------------------------------------------
Binary Search Method (Static Method In Array Class)
Reviewed by Baljeet Singh
on
07:01
Rating:
![Binary Search Method (Static Method In Array Class)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjS0dCScH7jbTz3MbwgRnng-VECDuzwK565rOhgsdMI5jOzc9kT-zpSrg8VGDLkLWxuBWICQwaC5zQefQgMDH3WAYg5dIV2l-uULP72PwQSffHublCfGWLXAneHNLTeAXVisqdmPRneixA9/s72-c/img.png)
No comments: