protected void btnTemp_Click(object sender, EventArgs e)
{
int[] intAry = new int[] { 8, 5, 3, 4, 8, 7, 2, 12, 50,
24, 20, 41, 21, 37, 40 };
intAry =
intAry.OrderBy(t => t).ToArray();
int index = BinarySearch(intAry, Convert.ToInt32(txtSec.Value));
}
int BinarySearch(int[] aryNum, int val)
{
int index = -1;
int start, end;
start = 0;
end =
aryNum.Length;
while (true)
{
int Mid = (end + start) / 2;
if (aryNum[Mid] == val)
{
index =
Mid;
return index;
}
else if (aryNum[Mid] > val)
{
end =
Mid - 1;
}
else if (aryNum[Mid] < val)
{
start =
Mid + 1;
}
if (start == end)
{
if (aryNum[start] == val)
{
index = start;
return index;
}
break;
}
}
return index;
}
No comments:
Post a Comment