+ 1
How can I do a binary search on an array ?
1 Réponse
+ 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DehnashiWindowsClassLibrary
{
public class Arrays
{
public static bool ArrayBinarySearch (string [] myArray,string str)
{
bool found=false;
Array.Sort(myArray);
int indexOfVar = Array.BinarySearch(myArray, str);
if (indexOfVar > -1)
{
found = true;
}
else
{
found = false;
}
return found;
}
public static bool ArrayBinarySearch(long [] myArray, long x)
{
bool found = false;
Array.Sort(myArray);
int indexOfVar = Array.BinarySearch(myArray, x);
if (indexOfVar > -1)
{
found = true;
}
else
{
found = false;
}
return found;
}
public static bool ArrayBinarySearch(double [] myArray, double x)
{
bool found = false;
Array.Sort(myArray);
int indexOfVar = Array.BinarySearch(myArray, x);
if (indexOfVar > -1)
{
found = true;
}
else
{
found = false;
}
return found;
}
}
}