当前位置:网站首页>Insertion sort (direct, binary)
Insertion sort (direct, binary)
2022-08-07 08:40:00 【HwWwWwK】
插入排序
哨兵的作用
- Save the copy before moving the location(The algorithm itself needs it)
- Determining boundaries reduces judgment(One less optimization of conditional judgment)
1. 直接插入排序
- 时间复杂度O(n2)
- 空间复杂度O(1)
- 稳定
- 适用:顺序存储和链式存储的线性表
视L[1 ~ n]List to be sorted,L[0]Get ready for a sentry position,目的是将LSorting is an increasing sequence
排序过程中,会出现 L[1 ~ i]有序,L[i+1 ~ n]待排,将L[i+1]插入L[1 ~ i]suitable location
int i, j;
for (i = 2; i <= n; i++) {
if (L[i] < L[i - 1]) {
L[0] = L[i]; // 哨兵
for (j = i - 1; L[0] < L[j]; j--) {
// j == 0时, must break out of the loop
L[j + 1] = L[j];
}
L[j + 1] = L[0]
}
}
Sometimes it can be awkward to use sentinels for provided arrays,The sentinel-free writing is given below.
int i, j, tmp;
for (int i = 1; i < n; i++) {
if (L[i] < L[i - 1]) {
tmp = L[i];
// j >= 0 Must be written in front,否则L[j]会越界
for (j = i - 1; j >= 0 && tmp < L[j]; j--) {
L[j + 1] = L[j];
}
L[j + 1] = tmp;
}
}
2. 折半插入排序
- 时间复杂度O(nlog2n)
- 空间复杂度O(1)
- 稳定
- Only lookups are optimized,Shift operations are not optimized
- 适用:顺序存储的线性表
视L[1 ~ n]List to be sorted,L[0]Get ready for a sentry position,目的是将LSorting is an increasing sequence
Direct insertion sort is a shift while searching,而由于L[1 ~ i]有序,You can find the right position by searching in half
int i, j;
int l, r, m;
for (int i = 2; i <= n; i++) {
if (L[i] < L[i - 1]) {
L[0] = L[i]; // 哨兵
l = 1, r = i; // [l,r) Left closed right open fold half to find
while (l < r) {
m = l + ((r - l) >> 1);
if (L[m] <= L[0]) l = m + 1;
else r = m;
}
// 第一个大于L[0]The numbers are subscripted asl或r,因为l==r
for (j = i - 1; j >= r; j--) {
L[j + 1] = L[j];
}
L[r] = L[0];
}
}
Sometimes it can be awkward to use sentinels for provided arrays,The sentinel-free writing is given below.
int i, j, tmp;
int l, r, m;
for (i = 1; i < n; i++) {
if (L[i] < L[i - 1]) {
tmp = L[i];
l = 0, r = i;
while (l < r) {
m = l + ((r - l) >> 1);
if (L[m] <= tmp) l = m + 1;
else r = m;
}
for (j = i - 1; j >= r; j--) {
L[j + 1] = L[j];
}
L[r] = tmp;
}
}
3. 希尔排序
- 时间复杂度依赖于增量序列的函数,当nin a certain range,时间复杂度约为O(n1.3),Worst degradation is O(n2)
- 空间复杂度O(1)
- 不稳定
- 适用:顺序存储的线性表
Change the original table to a different sync lengthdDivide into subtables,Then perform an insert sort on the child table.
d = 1时,Degenerates to direct insertion sort;But sorted by previous subtable,The original table has been relatively ordered,So sorting is faster.
int d, i, j, tmp;
for (d = n / 2; d >= 1; d >>= 1) {
for (i = d; i < n; i++) {
if (L[i] < L[i - d]) {
tmp = L[i];
for (j = i - d; j >= 0 && tmp < L[j]; j -= d) {
L[j + d] = L[j];
}
L[j + d] = tmp;
}
}
}
4. 例
// 直接插入排序
int* sortArray(int* nums, int numsSize, int* returnSize)
{
int i, j, temp;
*returnSize = numsSize;
for (i = 1; i < numsSize; i++) {
if (nums[i] < nums[i - 1]) {
temp = nums[i];
for (j = i - 1; j >= 0 && temp < nums[j]; j--) {
nums[j + 1] = nums[j];
}
nums[j + 1] = temp;
}
}
return nums;
}
// 折半插入排序
int* sortArray(int* nums, int numsSize, int* returnSize)
{
int i, j, temp;
int l, r, m;
*returnSize = numsSize;
for (i = 1; i < numsSize; i++) {
if (nums[i] < nums[i - 1]) {
temp = nums[i];
l = 0, r = i;
while (l < r) {
m = l + ((r - l) >> 1);
if (nums[m] <= temp) l = m + 1;
else r = m;
}
for (j = i - 1; j >= r; j--) {
nums[j + 1] = nums[j];
}
nums[r] = temp;
}
}
return nums;
}
// 希尔排序
int* sortArray(int* nums, int numsSize, int* returnSize)
{
*returnSize = numsSize;
int d, i, j, tmp;
for (d = numsSize / 2; d >= 1; d >>= 1) {
for (i = d; i < numsSize; i++) {
if (nums[i] < nums[i - d]) {
tmp = nums[i];
for (j = i - d; j >= 0 && tmp < nums[j]; j -= d) {
nums[j + d] = nums[j];
}
nums[j + d] = tmp;
}
}
}
return nums;
}
边栏推荐
- The baud rate of STM32 is wrong. The baud rate we want to set is 9600, but the actual baud rate is 14400. Why is this?
- 【正点原子STM32连载】第五章 STM32基础知识入门 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
- 力扣:1049. 最后一块石头的重量 II
- 背包理论之01背包
- 5 Crypto Themes Poised to Lead the Next Bull Market
- redis的原理和源码-sentinel哨兵的原理和源码解析(上)
- redis的原理和源码-配置文件解析
- 你如何看待抖音的中视频伙伴计划的?
- 一种API写法
- 家居江湖掀起「夺魁战」,红星美凯龙如何打造品牌增量场?
猜你喜欢

redis的原理和源码-sentinel哨兵的原理和源码解析(上)

org.apache.ibatis.binding.BindingException
![#yyds Dry Goods Inventory# [Yugong Series] August 2022 Go Teaching Course 004-Go Code Comments](/img/0e/a9e2757b0108621917f43d03ee9c26.png)
#yyds Dry Goods Inventory# [Yugong Series] August 2022 Go Teaching Course 004-Go Code Comments

FPGA开发第四弹:触摸按键控制LED灯实验

Scala——While和do..While循环控制

Jenkins configures automatic packaging

The second bullet of FPGA development: running water lamp experiment

Vitalik explains 5 types of ZK-EVM

redis的原理和源码-事务机制

Some basic concepts in networking
随机推荐
31. Understand what is avalanche, penetration and breakdown of redis?What happens after redis crashes?What are the countermeasures
Some basic concepts in networking
c语言const关键字
SRM系统是什么?有什么作用?企业如何应用SRM系统?
DCDC电源方案设计踩坑
Math和Date内置对象
E-commerce data warehouse notes 1 (data warehouse concept, project requirements and architecture design, data generation module)
RestTemplate
基于miniprogram-ci的微信小程序的CI以及接入钉钉通知
2022华数杯数学建模-在线文档
In-depth analysis of Spark SQL illustrates the execution process and application scenarios of five Join strategies
The principle and source code of redis - introduction of client structure and source code analysis
The PG function generates the corresponding table creation statement according to the table name
[Promise] Promise use / callback hell problem async-await / macro queue and micro queue
Sort -- bubble sort
力拓信创生态,博睿数据多款产品获得东方通与达梦数据库产品兼容互认证明
redis的原理和源码-redis的内存淘汰策略&LRU源码解析&LFU源码解析
Error f "pip[sys.version_info.major)" is reported after pip3 upgrade
如何配置百度地图应用访问白名单
redis的原理和源码-服务端的介绍和源码解析(命令请求的执行过程、服务器初始化过程)