static int Binary_Search(int[] a,int x) //二分法查找
{
int low = 0;
int high = a.Length-1;
int mid = 0;
while (low<=high)
{
mid = (low + high) / 2;
if (a[mid] == x)
return mid;
else if (a[mid] < x)
low = mid + 1;
else
high = mid – 1;
}
Console.WriteLine(“未找到” + x);
return -1;
}
static int Insert_Search(int[] a,int x) //插入法查找
{
int low = 0;
int high = a.Length-1;
int mid = 0;
while (low<=high)
{
mid = low + (x-a[low]) / (a[high]-a[low]) ;
if (a[mid] == x)
return mid;
else if (a[mid] < x)
low = mid + 1;
else
high = mid – 1;
}
Console.WriteLine(“未找到” + x);
return -1;
}
static void Bubble_Sort(int[] a) //改进冒泡排序
{
int count = 0;
Boolean flag = true;
for (int i = 0; i < a.Length && flag; i++)
{
flag = false;
for (int j = a.Length – 2; j >= i; j–)
{
if (a[j + 1] < a[j]) //这一行是关键
{
swap(a,j + 1,j);
count++;
flag = true;
}
}
}
Console.WriteLine(“交换次数=” + count);
}