排序 二分插入法排序

原文发布时间为:2009-03-06 —— 来源于本人的百度文章 [由搬家工具导入]

using System;//二分插入排序
namespace sorts
{
   public class Class4
    {
       public static void Main()
       {
           int[] a = new int[] { 2, 12, 6, 5, 4, 3, 9 };
           BinSort(a);
           for (int i = 0; i < a.Length; i++)
               Console.Write("{0} ", a[i]);
           Console.ReadLine();
       }

       public static void BinSort(int[] arr)
       {
           int left, right, mid, temp;

           for (int i = 1; i < arr.Length; i++)
           {
               left = 0;
               right = i - 1;
               temp = arr[i];
               while (left <= right)
               {
                   mid = (left + right) >> 1;//(left + right)/2
                   if (temp < arr[mid]) //升序
                       right = mid - 1;
                   else
                       left = mid + 1;
               }
               for (int j = i - 1; j >= left; j--)
                   arr[j + 1] = arr[j];
               arr[left] = temp;
           }
       }

    }
}

原文地址:https://www.cnblogs.com/handboy/p/7153326.html