输入一个数,在已经排序好的数组中查找是否存在和存在于数组什么位置

解决方案 »

  1.   

    设定x,y分别标注查找的开始位置和结束位置,然后如(x+y)/2这个中间元素比较,
    如果(x+y)/2>带查找元素(a)那么就在x--y(y = (x+y)/2)下查找
    反之则在x(x = (x+y)/2)--y下查找  这么简单的
      

  2.   

    int function(int a[] ,int b)

    }
      

  3.   

    namespace BinaryTree
    {
        class Program
        {
            static int  BinaryFind(int[] a, int b)
            {
                int low = 1;
                int high = a.Length;
                int mid;
                
                
                
                while (low <=high)
                {
                    mid = (low + a.Length)/2;
                    if (a[mid] > b)
                        high = mid-1;
                    else if (a[mid] < b)
                        low = mid+1;
                    else 
                        return mid+1;
                }
                return 0;        }
            static void Main(string[] args)
            {
                int[] a = new int[5] { 4, 6,9, 12, 18 };
                int store;
                store = Program.BinaryFind(a,12);
                if (Convert.ToBoolean(store))
                  Console.WriteLine("{0}", store);
                else
                    Console.WriteLine("没有要查找的数字");
            }
        }
    }
            }
            static void Main(string[] args)
            {
                int[] a = new int[5] { 4, 6, 19, 2, 8 };
                int store;
                store = Program.BinaryFind(a,8);
                if (Convert.ToBoolean(store))
                  Console.WriteLine("{0}", store);
                else
                    Console.WriteLine("没有要查找的数字");
            }
        }
      

  4.   

    http://blog.csdn.net/fengyarongaa/article/details/6563184