- #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;
- }
折半算法,留个影子
最新推荐文章于 2024-03-08 00:28:04 发布
6337

被折叠的 条评论
为什么被折叠?



