C#.net二分法查找的问题
public partial class _Default : System.Web.UI.Page{
private static int[] arrays = new int[10];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button3_Click(object sender, EventArgs e)
{
if (TextBox3.Text == "")
{
return;
}
else
{
TextBox4.Text = TextBox3.Text + " " + TextBox4.Text;
TextBox3.Text = "";
}
}
protected void Button2_Click(object sender, EventArgs e)
{
// string[] a = TextBox4.Text.Split(',');//将textbox中的值赋值给一个数组变量(其中textbox4中是一组数据)
//string[] str = TextBox4.Text.ToString().Split(new char[] { ' ', ' ', ' ', ' ', });
paixu();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
return;
}
else
{
int get = chazhao( arrays, int.Parse(textBox2.Text));
if (get == -1)
{
textBox2.Focus();
label1.Text = get.ToString();
return;
}
else
label1.Text = "查找数所在索引:" + get;
}
}
private void paixu()
{
int num, j;
string str = TextBox4.Text.ToString();
//arrays =str.Split ('');
string[] arrays = str.Split(' ');
for (int i = 1; i < arrays.Length-1; i++)
for (j = 0; j < arrays.Length - i-1; j++)
if (Int32.Parse(arrays[j]) > Int32.Parse(arrays[j + 1]))
{
num = Int32.Parse(arrays[j]);
arrays[j] = arrays[j + 1];
arrays[j + 1] = num.ToString();
}
for (int k = 0; k < arrays.Length; k++)
textBox1.Text = textBox1.Text + ' ' +arrays[k].ToString();
}
public static int chazhao(int[] a, int value)
{
int L = 0;
int H = a.Length - 1;
int middle = 0;
while (L <= H)
{
middle = (L + H) / 2;
if (a[middle] == value) { break; }
if (a[middle] < value) { L = middle + 1; }
if (a[middle] > value) { H = middle - 1; }
if (middle == H || middle == L)
middle = -1;
}
return middle;//没有则返回-1.
}
我只知道定义变量有问题。。查找的问题(chazhao 和Button1的代码有问题)。。(paixu)排序是没有问题的
源程序可以联系我的QQ2311388871 消息验证是.net我会加的,到时候发给你。谢谢
补充:.NET技术 , ASP.NET