原文链接:https://www.jb51.net/article/65006.htm
internal class Program { static void Main(string[] args) { Program program = new Program(); int[] ints = { 1, 2, 3, 4, 5 }; int i = program.BinarySearch(ints, 3); Console.WriteLine(i); Console.ReadKey(); } public int[] GetSubArray(int[] arr, int start, int end) { List<int> ints = new List<int>(); for (int i = start; i <= end; i++) { ints.Add(arr[i]); } return ints.ToArray(); } public int BinarySearch(int[] arr, int x) { if (arr.Length == 0) { return -1; } int mid = arr.Length / 2; if (arr[mid] == x) { return mid; } if (x < arr[mid]) { return BinarySearch(GetSubArray(arr, 0, mid - 1), x); } else { int indexFound = BinarySearch(GetSubArray(arr, mid + 1, arr.Length - 1), x); if (indexFound == -1) { return -1; } else { return mid + 1 + BinarySearch(GetSubArray(arr, mid + 1, arr.Length - 1), x); } } } }
标签:二分,program,Console,C#,int,查找,ints,Program From: https://www.cnblogs.com/zhu4c4/p/16990508.html