二分法是一个高效的算法,一般情况下,对于有序数据,这种查找就是最高效的算法了。
比如下面的数据:
Enter Number of Elements you Want to Hold in the Array ? 5 Enter Array Elements : 2 3 1 4 5 Sorted Array : 1 2 3 4 5 Enter the Element to be Searched : 4 Binary Search : 3 Element 3 is 4
输入的数据是无序,一定要进行有序排列。
例子如下:
/*
* C# Program to Perform Searching using Predefined Functions
*/
using System;
class linSearch
{
public static void Main()
{
Console.WriteLine("Enter Number of Elements you Want to Hold in the Array? ");
string s = Console.ReadLine();
int x = Int32.Parse(s);
int[] a = new int[x];
Console.WriteLine("Enter Array Elements :");
for (i