See the example below, it accepts two arguments: an array(students) and the value(“John Smith”) to be searched for
-------------------------------------------------------------------------
List<string> students = College.Students;
var result =Array.BinarySearch(students,“John Smith”);
-------------------------------------------------------------------------
http://msdn.microsoft.com/en-us/library/system.array.binarysearch.aspx
-------------------------------------------------------------------------
using System;
using System.Collections.Generic;
public static void BinarySearchName(ArrayList nameList)
{
CollectionBuilder cb = new CollectionBuilder();
ArrayList nameList = cb.GetNameCollection();
LinkedList<Person> persons = cb.GetPersonCollectionAsList();
string nameToFind;
Console.Write("\nEnter the name to find : ");
nameToFind = Console.ReadLine();
Console.WriteLine("\nStarting binary search\n");
int i = nameList.BinarySearch(nameToFind);
if (i < 0)
{
Console.WriteLine("Sorry the name " + nameToFind + " could not be found,\n");
}
else
{
Console.WriteLine("The name " + nameToFind + " was found");
}
}
-------------------------------------------------------------------------
No comments:
Post a Comment