标签:
两个有序数组找出相同数据,要求最简单的算法复杂度。
class Program { static void Main(string[] args) { int Low = 0; int[] m = new int[] { 2, 4, 6, 9, 12, 13, 15, 16 }; int[] n = new int[] { 3, 5, 9, 12, 15 }; foreach (int item in m) { Search(n, ref Low, n.Length - 1, item); } Console.ReadKey(); } public static void Search(int[] Array, ref int Low, int High, int key) { if (Low <= High) { int Middle = (High + Low) / 2; if (Array[Middle] == key) { Low = Middle + 1; Console.WriteLine(Array[Middle]); } else if (Array[Middle] > key) { Search(Array, ref Low, Middle - 1, key); } else { Low = Middle + 1; Search(Array, ref Low, High, key); } } } }
标签:
原文地址:http://www.cnblogs.com/imhaiyang/p/4978119.html