1
如果我搜索我的排序數組,它只會顯示其中一個值,即使數組中有多個相同的值。我不想告訴我有多少重複,我希望它顯示我搜索的數組中的所有重複值。還是有不同的搜索我需要用來做到這一點?C#獲取二進制搜索以顯示數組中的所有重複值
所以,如果我有數組1 {1,2,3,4,4,5,5,5,5,6},我尋找5 I希望它輸出:
5
5
5
5
這是是我的二進制搜索代碼。
public class search
{
public static void Main(string[] args)
{
//Arrays are created here. e.g. array1{1,2,3,4,4,5,5,5,5,6}
int Input;
Console.WriteLine("Enter the number you would like to search for.");
Input = Convert.ToInt32(Console.ReadLine());
int y = BinarySearch(array1, Input);
Console.WriteLine("array1 {0} : array2{1} : array3 {2} : array4 {3} : array5 {4}",array1[y], array2[y], array3[y], array4[y], array5[y]);
}
public static int BinarySearch(double[] Array, int Search)
{
int x = Array.Length;
int low = 0;
int high = x - 1;
while (low <= high)
{
while (low <= high)
{
int mid = (low + high)/2;
if (Search < Array[mid])
{
high = mid - 1;
}
else if (Search > Array[mid])
{
low = mid + 1;
}
else if (Search == Array[mid])
{
Console.WriteLine("{0}", Search);
return mid;
}
}
Console.WriteLine("{0} was not found.", Search);
}
return high;
}
}
你有這樣的'return'聲明第一場比賽 –