标签:
#include<iostream> using namespace std; #define SIZE 21 typedef int Sqlist[SIZE]; void BInsertSort(Sqlist &L,int n) { int i,j; int low; int high; int mid; for(i=2;i<n;++i) { L[0] = L[i]; low = 1; high = i; while(low <= high) { mid = (low + high)/2; if(L[0] > L[mid]) low = mid+1; else high = mid-1; } for(j = i;j > high+1 ;--j) { L[j] = L[j-1]; } L[high+1] = L[0]; } } void main() { Sqlist L= {0,49,38,65,97,76,13,27}; BInsertSort(L,8); for(int i=1;i<8;++i) cout<<L[i]<<" "; cout<<endl; }
分析:折半插入排序法相对于直接插入法,减少了比较的次数,但是没有减少移动的次数,没有很好的优化直接插入法。
标签:
原文地址:http://blog.csdn.net/zr1076311296/article/details/46546585