当前位置:网站首页>带你了解直接插入排序(C语言)
带你了解直接插入排序(C语言)
2022-06-11 11:41:00 【Butayarou】
引入:
其实它和玩扑克牌的原理是一样的(用升序来举例):先拿第一张牌,再拿第二张牌,从后往前比较,此时只能与第一张牌比较(如果第二张牌的点数更大,把它放在第一张牌的后面,否则放在前面),然后拿第三张牌,将它从后往前比较,把它插入到合适的位置,使手上的牌仍然保持升序,接下来的牌都跟前面的操作相同。
基本思路:让 end 指向第一个数,先用 tmp 保存下标为(end + 1)的数,再将 tmp 依次与
下标从 end 到 0 的数(从后往前)比较(符号方向依是升序还是降序来定,且该步需要挪动元素),把 tmp 存的值插入到合适的位置。
将上面的基本思路做成一个循环(让 end 的指向范围为 [ 0 , n-1 ] )
可以发现,
手上现有的牌 ---------------->下标从 0 到 end 的元素tmp ----------------> 新拿的一张牌
从后往前比较,将牌插入到合适的位置 ----------------> 将 tmp 与 下标从 end 到 0
的元素比较(需要挪动元素),把 tmp 存的值插入到合适的位置
于是,每执行完一次循环后,下标从 0 到 end 的元素都是有序的。
代码实现
void InsertSort(int* a,int n)
{
for (int i = 0; i < n - 1;i++)
{
int end = i; //赋值给end,使得end位置后移,外扩
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--; //靠end后退来实现两数比较
}
else
break;
}
a[end + 1] = tmp;
}
}
边栏推荐
- CVPR 2022 𞓜 text guided entity level image manipulation manitrans
- The tutor transferred me 800 yuan and asked me to simulate a circuit (power supply design)
- WordPress database cache plug-in: DB cache Reloaded
- File excel export
- 统计出现次数最多的前K个字符串
- Interview experience of Xiaomi Android development post~
- Guangdong municipal safety construction data management software 2022 new forms are coming
- JS interview questions - arrow function, find and filter some and every
- golang利用异或^交换两个变量以及加解密
- 軟件項目管理 7.1.項目進度基本概念
猜你喜欢

ELK - Hearthbeat实现服务监控

JS interview questions - arrow function, find and filter some and every

Maximum water container

安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码

PS does not display text cursor, text box, and does not highlight after selection

The complete manual of the strongest Flink operator is a good choice for the interview~
![[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)](/img/30/79516390c2b2b50a224eaa84a0c1c9.jpg)
[file upload vulnerability 06] server file content detection and bypass experiment + image horse production method (based on upload-labs-14 shooting range)

Intl.numberformat set number format

Streaking? Baa!

Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决
随机推荐
Centos7.x下安装mysql8遇到的问题Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
Use cache to reduce network requests
Full Permutation (recursion, backtracking)
National multi-year solar radiation spatial distribution data 1981-2022, temperature distribution data, evapotranspiration data, evaporation data, rainfall distribution data, sunshine data, wind speed
my. Binlog startup failure caused by the difference between [mysql] and [mysqld] in CNF
Etcd介绍
Smart sidebar plug-in: Mo widgets
Learning in Bi design 03
在WordPress媒体库中创建文件夹
Uncaught typeerror: cannot set property 'next' of undefined
Streaking? Baa!
快速搭建ELK7.3
[JUC supplementary] atomic class, unsafe
Elk - x-pack set user password
导师转我800块,让我仿真一个电路(电源设计)
[JUC supplementary] immutable object, shared meta mode, final principle
PS does not display text cursor, text box, and does not highlight after selection
JS interview questions - arrow function, find and filter some and every
[issue 30] shopee golang development experience
刷题笔记(十四)--二叉树:层序遍历和DFS,BFS