源文件:http://pan.baidu.com/share/link?shareid=439727&uk=3912660076
代码参考来源于课本:
//Main:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BinarySearch { class Program { static void Main(string[] args) { Function obj = new Function(); int position; Console.WriteLine("The array is:"); Console.WriteLine(obj); while (true) { Console.WriteLine("Please choose:"); Console.WriteLine("1.BinarySerach;"); Console.WriteLine("2.RecursiveBinarySearch;"); Console.WriteLine("3.Exit;"); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); switch (number) { case 1: Console.WriteLine("Please enter an integer target value:"); int targetValue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); position = obj.BinarySerach(targetValue); if (position == -1) Console.WriteLine("The integer {0} was not found.\n", targetValue); else Console.Write( "The integer {0} was found in position:{1}\n", targetValue, position); break; case 2: Console.WriteLine("Please enter an integer target value:"); int targetValue2 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); position = obj.RecursiveBinarySearchHelp(targetValue2); if (position == -1) Console.WriteLine("The integer {0} was not found.\n", targetValue2); else Console.Write( "The integer {0} was found in position:{1}\n", targetValue2, position); break; case 3: Environment.Exit(0); break; } } } } }
//Class:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BinarySearch { class Function { /// <summary> /// 声明变量 /// </summary> private int[] array; private static Random ran = new Random(); /// <summary> /// 初始化数组 /// </summary> /// <param name="length"></param> public Function() { int length = ran.Next(1, 38); array = new int[length]; while (length > 0) array[--length] = ran.Next(0, 100); //折半查找要求数组状态为排好序的 Array.Sort(array); } /// <summary> /// 折半查找: /// 折半查找要求数组先排好序,将数组划分为三部分左中右部分,中间只有一个元素。 /// 第一次迭代测试数组中的中间部分,如果匹配返回位置结束,否则与中间元素比较, /// 若小于中间元素说明要查找的元素在数组的左边,反之在右边,确定在哪边后,放弃另一边和中间. /// 再把只有这边的元素划分为三部分,进行类推......直到数组只有一个元素为止。返回结果, /// 核心算法时间复杂度: /// T(n)=O(㏒n) /// </summary> /// <param name="targetValue"></param> /// <returns></returns> public int BinarySerach(int targetValue) { //初始假定没找到返回位置为-1; int position = -1; // int left, middle, right; //初始为划分数组中为三段 //最左边, left = 0; //中间 middle = array.Length / 2; //最右边 right = array.Length - 1; do {//每划分一次就展示一次划分情况 Console.Write(RemainingElements(left, right)); for (int i = 0; i < middle; i++) Console.Write(" "); Console.WriteLine(" * "); //目标元素刚好就是中间的元素返回其位置 if (targetValue == array[middle]) return position = middle; //目标元素小于中间的元素==在数组的左边。改变划分范围最右边变为原来的左边的最右边。 else if (targetValue < array[middle]) right = middle - 1; else //目标元素大于中间的元素==在数组的右边。改变划分范围最左边变为原来的右边的最左边。 left = middle + 1; //重新初始化middle middle = (left + right) / 2; } while (left <= right);//至最后只有一个元素为止 //返回结果 return position; } /// <summary> /// 辅助代码 /// </summary> /// <param name="targetValue"></param> /// <returns></returns> public int RecursiveBinarySearchHelp(int targetValue) { return RecursiveBinarySearch(0, array.Length - 1, targetValue); } /// <summary> /// 折半查找的递归算法 /// </summary> /// <param name="left"></param> /// <param name="right"></param> /// <param name="targetValue"></param> /// <returns></returns> public int RecursiveBinarySearch(int left, int right, int targetValue) { //初始假定没找到返回位置为-1; int position = -1; //没有找到 if (left > right) return position; // int middle = (left + right) / 2; // Console.Write(RemainingElements(left, right)); for (int i = 0; i < middle; i++) Console.Write(" "); Console.WriteLine(" * "); // if (targetValue == array[middle]) return position = middle; else if (targetValue < array[middle]) position = RecursiveBinarySearch(left, middle - 1, targetValue); else position = RecursiveBinarySearch(middle + 1, right, targetValue); // return position; } /// <summary> /// 展示 /// </summary> /// <param name="low"></param> /// <param name="high"></param> /// <returns></returns> public string RemainingElements(int low, int high) { string temporary = string.Empty; // output spaces for alignment for (int i = 0; i < low; i++) temporary += " "; // output elements left in array for (int i = low; i <= high; i++) temporary += array[i] + " "; temporary += "\n"; return temporary; } // end method RemainingElements // method to output values in array public override string ToString() { return RemainingElements(0, array.Length - 1); } } }
//运行结果截图: