算法之【折半插入法】

折半插入排序(binary insertion sort)是对插入排序算法的一种改进,采用二分法进行比较时不用一个一个比,而是”跳着选”的方式.

Java算法原型:

void BinaryInsertSort(int R[],int n )

{

    int i,j,mid,low,high,temp;

    for(i=2; i<=n; ++i)

    {

        R[0] = R[i];

        low = 1;

        high = i-1;

        while(low <= high)

        {

            mid = (low + high) / 2;

            if(temp > R[mid])

            {

                low = mid + 1;

            }else{

                high = mid - 1;

            }

        }

        for(j=i-1; j>=high+1; --j)

        {

            R[j+1] = R[j];

        }

        R[high+1] = R[0];

    }

}

posted @ 2017-09-04 10:15  xosg  阅读(166)  评论(0编辑  收藏  举报