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:
No comments: