+ 1

How can I do a binary search on an array ?

27th Oct 2016, 8:40 PM
MohammadReza Dehnashi
MohammadReza Dehnashi - avatar
1 Antwort
+ 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; } } }
27th Oct 2016, 8:42 PM
MohammadReza Dehnashi
MohammadReza Dehnashi - avatar