#include <iostream> using namespace std; template <class elemtype> void bInsertSort(elemtype *L,int length) { elemtype watcher; for(int i =1;i<length;i++) { watcher = L[i];// 放入岗哨 // 用折半查找法寻找擦汗如位置 int low =0; int high =i-1; int mid; if (L[high ]<=watcher) continue;//当顺序正好时,不需要移动 while (low <= high) { mid =(low+high)/2; if(L[mid] < watcher) low =mid+1; else // 不论他们相等与否 high =mid -1; } //插入数据,需要后移 // for (int j =i;j>mid;j--)// -----------输出结果错误: 都为 1 for(int j =i;j>high;j--) { L[j]=L[j-1]; } L[high+1] =watcher; } } int main() { int a[5]={1,4,2,5,3}; bInsertSort(a,5); cout<<" 排序结果如下 :"<<endl; for (int i =0;i<5;i++) { cout<<" "<<a[i]; } cout<<endl; return 0; } 后续将完善 算法分析: