两个有序数组找出相同数据

两个有序数组找出相同数据,要求最简单的算法复杂度。

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);
                }
            }
        }
    }

 

posted @ 2015-11-19 16:46  马羊  阅读(1167)  评论(0编辑  收藏  举报