冒泡排序+二分之查找
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 二分支查找
{
class Program
{
static void Main(string[] args)
{
int[] data = new int[] { 3, 0, 4, 3, 7 };
Console.WriteLine(Get(data, -1));
Console.ReadLine();
}
public static int Get(int[] data, int a)
{
int position = 1;
//冒泡排序
for (int i = 0; i < data.Length; i++)
{
for (int j = i; j < data.Length; j++)
{
if (data[i] > data[j])
{
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
int index;
int endIndex = data.Length;
int beginIndex = 0;
index = (beginIndex + endIndex) / 2;
for (int j = 0; j < data.Length; j++)
{
if (a < data[1])
{
position = 1;//返回值
}
else if (a > data[data.Length - 1])
{
position = data.Length + 1;//返回值
}
else
{
if (a == data[index] && a == data[index - 1])
{
position = index;//返回值
}
else if ((a < data[index] && a > data[index - 1]) || a == data[index])
{
position = index + 1;//返回值
}
else if (a < data[index - 1])
{
endIndex = index - 1;
}
else if (a > data[index + 1])
{
beginIndex = index + 1;
}
index = (beginIndex + endIndex) / 2;
}
}
return position;
}
}
}