C++
 #define _CRT_SECURE_NO_WARNINGS
 #include <iostream>
 using namespace std;
 #define LEN 8 // 有LEN个元素要排
 struct Record { // 为了考察排序的稳定性,定义元素是结构体类型
 int key;
 int otherinfo;
 };
 void BInsertSort(Record *arr, int length) // length是要排序的元素的个数,0号单元除外
 {
 for (int i = 2; i <= length; ++i) {
 arr[0] = arr[i]; // 将arr[i]暂存到arr[0]
 int low = 1;
 int high = i - 1;
 while (low <= high) { // 在arr[low..high]中折半查找有序插入的位置
 int m = (low + high) / 2; // 折半
 if (arr[0].key < arr[m].key) // 关键字相同时,使low = m + 1,到高半区,保证稳定性
 high = m - 1; // 插入点在低半区
 else
 low = m + 1; // 插入点在高半区
 }
 for (int j = i - 1; j >= high + 1; --j)
 arr[j + 1] = arr[j]; // 记录后移
 arr[high + 1] = arr[0]; // 插入
 }
 }
 int main(void)
 {
 freopen("in.txt", "r", stdin);
 Record a[LEN + 1] = {0};
 for (int i = 1; i <= LEN; i++)
 cin >> a[i].key >> a[i].otherinfo;
 BInsertSort(a, LEN);
 for (int i = 1; i <= LEN; i++)
 cout << a[i].key << '\t' << a[i].otherinfo << endl;
 return 0;
 }
 /*
 in.txt:
 49 1
 38 0
 65 0
 97 0
 76 0
 13 0
 27 0
 49 2
 out:
 13 0
 27 0
 38 0
 49 1
 49 2
 65 0
 76 0
 97 0
 */
JAVA
 public class MyBinaryInsertionSort
 {
 public static void main(String[] args)
 {
 // 待排序的数组
 int[] array = { 1, 0, 2, 5, 3, 4, 9, 8, 10, 6, 7};
 binaryInsertSort(array);
 // 显示排序后的结果。
 System.out.print("排序后: ");
 for(int i = 0; i < array.length; i++)
 {
 System.out.print(array[i] + " ");
 }
 }
 // Binary Insertion Sort method
 private static void binaryInsertSort(int[] array)
 {
 for(int i = 1; i < array.length; i++)
 {
 int temp = array[i];
 int low = 0;
 int high = i - 1;
 while(low <= high)
 {
 int mid = (low + high) / 2;
 if(temp < array[mid])
 {
 high = mid - 1;
 }
 else
 {
 low = mid + 1;
 }
 }
 for(int j = i; j >= low + 1; j--)
 {
 array[j] = array[j - 1];
 }
 array[low] = temp;
 }
 }
 }
C语言
 #include <stdio.h>
 typedef int ElemType ;
 ElemType arr[]={0,9,8,7,6,5,4,3,2,1};
 ElemType n=10,i,j;
 ElemType low,high,mid;
 void BinSort(ElemType r[],ElemType n)
 {
 for(i=2;i<n;i++)
 {
 r[0]=r[i];
 low=1; high=i-1;
 while (low<=high)
 {
 mid=(low+high)/2;
 if(r[0]<r[mid])
 high=mid-1;
 else
 low=mid+1;}
 for(j=i-1;j>=low;j--)
 {
 r[i]=r[j];
 i--;
 }
 r[low]=r[0];} }
 void put(ElemType r[],ElemType n)
 {
 for(j=1;j<n;j++)
 printf("%d\t",r[j]);
 printf("\n");
 }
 void main()
 {
 BinSort(arr,n);
 put(arr,n);
 }