折半插入排序_i++


折半插入排序_i++_02


折半插入排序_折半_03


折半插入排序_i++_04

class Program
{
static void Main(string[] args)
{

int[] sum = { 0, 20, 50, 8, 12, 1, 400, 522 };
BinaryInsertSort(sum);
for (int i = 1; i < sum.Length; i++)
{
Console.Write(sum[i] + " ");
}

Console.ReadKey();
}
/// <summary>
/// 折半插入
/// </summary>
static void BinaryInsertSort(int[] array)
{
for (int i = 2; i < array.Length; i++)
{
array[0] = array[i];
int low = 1;
int high = i - 1;
while (low<=high)
{
int mid = (low + high) / 2;
if (array[mid] > array[0])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
//high+1的位置正是要插入的位置
for (int j = i-1; j>=high+1; j--)
{
array[j + 1] = array[j];
}

array[high + 1] = array[0];
}
}
}