当前位置:网站首页>折半插入排序
折半插入排序
2022-07-27 14:27:00 【发发是只呆头鹅】
1.折半插入排序
因为折半查找比直接查找更快,所以当需要排序的数字越多时,折半插入排序相较于直接插入排序效率更高。
2.算法思想
采用折半查找招待待排序元素的插入位置
3.代码实现
#include <stdio.h>
void display(int arr[],int size)
{
for(int i=1;i<size;i++)
printf("%d ",arr[i]);
printf("\n");
}
void BInsertSort(int arr[],int size)
{
int i,j,low,high,mid;
for(i=2;i<size;i++)
{
low=1;
high=i-1;
arr[0]=arr[i];
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]>arr[0])
high=mid-1;
else
low=mid+1;
} //high+1就是插入位置
for(j=i-1;j>=high+1;j--)
arr[j+1]=arr[j];
arr[high+1]=arr[0];
}
}
int main()
{
int arr[]={
0,5,4,3,2,1};
int size=sizeof(arr)/sizeof(arr[0]);
display(arr,size);
BInsertSort(arr,size);
display(arr,size);
return 0;
}
4.运行结果
5.分析
该段代码中需要注意的有以下几点:
1.数组arr中的第一个元素是0,作为哨兵使用,不存放具体数据。
2.对于插入位置为high+1是为什么呢?首先对于while循环中,最后一次循环的情况是low=high,此时low,high,mid三个指向同一个位置,当arr[mid]>arr[0]时,high的值会减1,此时需要插入的值比arr[mid]小,所以需要将arr[mid]向后移动一个位置,所以high+1就是要插入的位置;当arr[mid]<=arr[0]时,low的值会加1,此时arr[mid]小于或等于需要插入的值,即需要插入的值要放在arr[mid]后面,即hight+1的位置,综上两种情况,我们会发现插入位置还可以是low。
3.该算法是稳定的,判断稳定的条件是,出现相同的数字时,其排序的相对位置和原来一样,该代码中出现相同元素的情况为while循环中的else情况,即arr[mid]<=arr[0],此时要插入的元素依然会插入到arr[mid]的后面,所以相对位置没有改变,即是稳定的。
边栏推荐
- 复杂度分析
- Unity mouse controls the first person camera perspective
- Leetcode 240. search two-dimensional matrix II medium
- C:浅谈函数
- Network equipment hard core technology insider router Chapter 4 Jia Baoyu sleepwalking in Taixu Fantasy (Part 2)
- 设置提示框位置随鼠标移动,并解决提示框显示不全的问题
- 【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
- [0 basic operations research] [super detail] column generation
- EMC design scheme of USB2.0 Interface
- 【剑指offer】面试题54:二叉搜索树的第k大节点
猜你喜欢

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

/dev/loop1占用100%问题

Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)

npm install错误 unable to access

With just two modifications, apple gave styleganv2 3D generation capabilities

Dan bin Investment Summit: on the importance of asset management!

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

C语言:字符串函数与内存函数

Leetcode 190. reverse binary bit operation /easy

【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
随机推荐
The design method of integral operation circuit is introduced in detail
【剑指offer】面试题54:二叉搜索树的第k大节点
Two stage submission and three stage submission
Pytorch replaces some components in numpy. / / please indicate the source of the reprint
Spark Filter算子在Parquet文件上的下推
Database: use the where statement to retrieve (header song)
Use double stars instead of math.pow()
【剑指offer】面试题55 - Ⅰ/Ⅱ:二叉树的深度/平衡二叉树
Summer Challenge harmonyos realizes a hand-painted board
[正则表达式] 单个字符匹配
Spark3中Catalog组件设计和自定义扩展Catalog实现
Deveco studio2.1 operation item error
Leetcode 190. reverse binary bit operation /easy
Leetcode 341. flattened nested list iterator DFS, stack / medium
聊聊面试必问的索引
QT (IV) mixed development using code and UI files
Spark Bucket Table Join
HaoChen CAD building 2022 software installation package download and installation tutorial
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
flutter —— 布局原理与约束