binaryinsertsort.h

来自「随机产生个整数n=100」· C头文件 代码 · 共 29 行

H
29
字号
template<class T>
void BinaryInsertSort(T a[],int N,int&KCN,int&RMN)
{
    KCN=0 ;
    RMN=0 ;
    for(int i=1;i<N;i++)
    {
        T temp=a[i];
        RMN++;
        int low=0,high=i-1 ;
        //折半查找
        while(low<=high)
        {
            int mid=(low+high)/2 ;
            if(++KCN&&temp<a[mid])high=mid-1 ;
            else low=mid+1 ;
        }
        for(int j=i-1;j>=low;j--)
        {
            a[j+1]=a[j];
            RMN++;
        }
        //记录后移
        a[low]=temp ;
        RMN++;
        //插入
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?