#include <iostream>
using namespace std;
int main()
{
//折半插入查找
int R[] = {38, 38, 38, 65, 76, 7, 45, 49};
int n = 8;
for (int i = 1; i < n; ++i)
{
int temp = R[i];
int low = 0;
int high = i - 1;
int pos;
while (high >= low)
{
pos = (low + high) / 2;
if (R[pos] < R[i])
low = pos + 1;
else
high = pos - 1;
}
//待排序的数字插入的位置是:high后一位
//把high+1位置到i-1位置的数字依次向后移动一位,最后把待排序数字填入high+1位置
for (int j = i; j >= high + 2; --j)
{
R[j] = R[j - 1];
}
R[high + 1] = temp;
}
for (int i = 0; i < n; ++i)
{
cout << R[i] << endl;
}
return 0;
}
排序算法2--插入排序2--折半插入排序(C++完整代码实现)
最新推荐文章于 2023-01-29 16:56:25 发布