当前位置:网站首页>Take you to know about direct insertion sorting (C language)
Take you to know about direct insertion sorting (C language)
2022-06-11 11:56:00 【Butayarou】
introduce :
In fact, it is the same principle as playing cards ( Take ascending order as an example ): Take the first card first , Take the second card , Compare backwards and forwards , At this time, it can only be compared with the first card ( If the second card has more points , Put it behind the first card , Otherwise, put it in front ), Then take the third card , Take it Compare backwards and forwards , Put it in place , Keep your cards in ascending order , The next cards are the same as the previous ones .
The basic idea : Give Way end Point to the first number , First use tmp Save subscript as (end + 1) Number of numbers , then tmp And, in turn,
Subscript from end To 0 Number of numbers ( From back to front ) Compare ( The direction of symbols depends on whether they are in ascending or descending order , And this step needs to move the element ), hold tmp Insert the stored value into the appropriate position .
Make the above basic idea into a cycle ( Give Way end The pointing range of is [ 0 , n-1 ] )
You can find ,
Existing cards in hand ----------------> Subscript from 0 To end The elements oftmp ----------------> A new card
Compare backwards and forwards , Insert the card into the right place ----------------> take tmp And Subscript from end To 0
Compare the elements of ( Need to move elements ), hold tmp Insert the stored value into the appropriate position
therefore , After each execution of the loop , Subscript from 0 To end All the elements are ordered .
Code implementation
void InsertSort(int* a,int n)
{
for (int i = 0; i < n - 1;i++)
{
int end = i; // Assign a value to end, bring end Move back , Outspread
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--; // by end Backward to compare two numbers
}
else
break;
}
a[end + 1] = tmp;
}
}
More articles
Let you understand the sorting of choices (C Language )
Let you understand bubble sort (C Language )
边栏推荐
- PS does not display text cursor, text box, and does not highlight after selection
- 2019年书单
- Iframe value transfer
- Intermediate web development engineer, interview questions + Notes + project practice
- C# 在PDF文档中应用多种不同字体
- How to understand CPU load
- iframe 传值
- Etcd的运行时重配置
- Notes on brushing questions (13) -- binary tree: traversal of the first, middle and last order (review)
- Test cos HTML cache static cache plug-in
猜你喜欢

Apple mobileone: the mobile terminal only needs 1ms of high-performance backbone

js面试题---箭头函数,find和filter some和every

让你理解选择排序(C语言)

JS 加法乘法错误解决 number-precision

Node connects to MySQL database and writes fuzzy query interface

再不刷题就晚了,最全的BAT大厂面试题整理

解决swagger文档接口404的问题

Use compiler option ‘--downlevelIteration‘ to allow iterating of iterators 报错解决

Is the SSL certificate reliable in ensuring the information security of the website?

Use of RadioButton in QT
随机推荐
JS 加法乘法错误解决 number-precision
2020-07 学习笔记整理
快速搭建ELK7.3
golang利用异或^交换两个变量以及加解密
What is the latest popular annuity insurance product with higher income in 202?
CVPR 2022 𞓜 text guided entity level image manipulation manitrans
Uncaught typeerror: cannot set property 'next' of undefined
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
AGCO AI frontier promotion (6.11)
ELK - ElastAlert最大的坑
JS to realize the rotation chart (riding light). Pictures can be switched left and right. Moving the mouse will stop the rotation
统计出现次数最多的前K个字符串
[JUC supplementary] immutable object, shared meta mode, final principle
[JUC supplementary] atomic class, unsafe
Recommend several gravatar avatar caching plug-ins
C# 设置或验证 PDF中的文本域格式
Is the SSL certificate reliable in ensuring the information security of the website?
2019 book list
安全工程师发现PS主机重大漏洞 用光盘能在系统中执行任意代码
Count the top k strings with the most occurrences