Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Binary Search in Array Using Console Application in C#

Author: DevASP
Download Source Code : 442_ConsoleApplication1.zip

In this simple article we will see that how we can implement binary search in our console application.

Simply create a C# console application and write simple code in its main function.

 

int [] intArray = new int[3] {0, 1, 2};

string[] names = new string[] {"Jeff","Tim", "Smith","Alan"};

object obj1 = "";

object obj2 = 1;

                 

Console.WriteLine("Please enter the String Which you want to search");

obj1=Console.ReadLine();

                 

int retVal = Array.BinarySearch(names, obj1);

if(retVal >=0)

      Console.WriteLine("Item index " +retVal.ToString() );

else

      Console.WriteLine("Item not found");

 

retVal = Array.BinarySearch(intArray, obj2);

                 

if(retVal >=0)

      Console.WriteLine("Item index " +retVal.ToString() );

else

      Console.WriteLine("Item not found");

                 

System.Threading.Thread.Sleep(10000);

Console class is use to call methods of console class like write read etc

Console.WriteLine

this is use to send output on screen.

 

Console.ReadLine

this is use to get input from user.

Array.BinarySearch

This is use For Binary Search in a Array.

Article Comments
hello,
Sir your written code is not clearly know to the freshers.there are the no method and decleration too good.plz provide me simple source code in C# for all shorting algorithm..

Posted on 7/22/2010 5:39:47 AM by avnish

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working With Arrays in c#.

Disclaimer - Privacy
© 2002-2017 DevASP.net