本帖最后由 fishrd 于 2010-10-26 12:02:25 编辑

解决方案 »

  1.   

    1) while (low != high) 改成 while(low <= high)
       return 0; 改成 return -1;//表示找不到2) MessageBox
     private void button1_Click(object sender, EventArgs e)//开始查找
     {
              int data = id;
              int[] array= {1,2,3};
              int location = search(id,array);
              if(location>=0)
              {
                MessageBox.Show("找到的提示");
              }        
              else 
              {
                MessageBox.Show("找不到的提示");
              }
     }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace search
    {
        public partial class Form1 : Form
        {
            private int id;        public Form1()
            {
                InitializeComponent();
            }        private void textBox1_TextChanged(object sender, EventArgs e)//输入查找数据 
            {
               id = Convert.ToInt32(textBox1.Text);
            }        private void button1_Click(object sender, EventArgs e)//开始查找
            {
               int data = id;
               int[] array = { 1, 2, 3 ,4};//数组列表 
               int location = search(id, array);
               if (location >= 0)
               {
                   MessageBox.Show("....", "查找结果");//显示查找到的数据
               }
               else
               {
                   MessageBox.Show("表中不存在您要查找的数据", "查找结果");
               }
            }       private int search(int data, int[] array)//二分法
            {           
                int low = 0;
                int high = array.Length;
                int mid;
                while (low != high)
                {
                    mid = (low + high) / 2;
                    if (array[mid] == data) return data;//返回查找到的值
                    else
                    {
                        if ((array[mid] > data && array[low] < data) || (array[mid] < data && array[high] < data))
                        { high = mid - 1; }
                        else if ((array[mid] < data && array[high] > data) || (array[mid] > data && array[low] > data))
                        { low = mid + 1; }
                    }
                 }
                 return -1;
              } 
          }
     }
    修改后的代码 请问如何才能将查找出来的data在messagebox中显示出来
      

  3.   

    你的Data是int型的数据,MessageBox里也只有转成string 再显示咯private void button1_Click(object sender, EventArgs e)//开始查找
            {
               int data = id;
               int[] array = { 1, 2, 3 ,4};//数组列表 
               int location = search(id, array);
               if (location >= 0)
               {
                   MessageBox.Show(location.ToString()+被查找出来!, "查找结果");//显示查找到的数据
               }
               else
               {
                   MessageBox.Show("表中不存在您要查找的数据", "查找结果");
               }
            }
      

  4.   

    linq from q in arr where ..查找很方便
    二分法数据结构上算法很全面