当前位置:网站首页>Unable to climb hill sort, directly insert sort
Unable to climb hill sort, directly insert sort
2022-07-01 22:54:00 【Old fish 37】
Sorry for breaking the final exam for so long , From today on, an average of one high-quality article is output every day for everyone to enjoy !!!
First:
Direct insert sort :InsertSort
Holistic thinking :

Complete code implementation :
// Direct insert sort
void InsertSort(int* arr, int len)
{
for (int i = 0; i < len - 1; i++)
{
int end = i;
int tmp = arr[end + 1];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + 1] = arr[end];
--end;
}
else
{
break;
}
}
arr[end + 1] = tmp;
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
InsertSort(arr, len);
PrintSort(arr, len);
}The following is about how you once ignored him , Now you can't afford Hill sort
Hill sort is an optimization of direct insert sort
Holistic thinking :

1、 Pre sort --- Turn data into data that is about to be sorted
step : Divide into groups first gap, And then we can sort it

When gap arrive 1 It's sorted when
Isn't that amazing
Summarize the characteristics of hill sorting :
Complete code implementation :
// Shell Sort
void ShellSort(int* arr, int len)
{
int gap = len;
while (gap > 1)
{
gap = gap / 3 + 1;// Make sure the last number is 1
for (int i = 0; i < len - gap; i++)
{
int end = i;
int tmp = arr[end + gap];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + gap] = arr[end];
end -= gap;
}
else
{
break;
}
}
arr[end + gap] = tmp;
}
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
ShellSort(arr, len);
PrintSort(arr, len);
}Guys, hurry up and operate the machine !!!
If there is a mistake , Your advice are most welcome !
边栏推荐
- [literacy] deep / shallow, local / global features in machine learning image processing
- 【QT小作】封装一个简单的线程管理类
- 转载csdn文章操作
- Metauniverse may become a new direction of Internet development
- Electron学习(三)之简单交互操作
- 激发新动能 多地发力数字经济
- SAP 智能机器人流程自动化(iRPA)解决方案分享
- Sogou wechat app reverse (II) so layer
- rxjs Observable of 操作符的单步调试分析
- Efficiency improvement - encourage personalized container development environment
猜你喜欢

104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
![[image segmentation] 2021 segformer neurips](/img/2f/a8631cbe9a46419b8dbd5205e1f5b5.png)
[image segmentation] 2021 segformer neurips

El input text field word limit, beyond which the display turns red and input is prohibited

使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏

Map container

思科考试--冗余网络

激发新动能 多地发力数字经济
![[jetcache] how to use jetcache](/img/fa/5b3abe53bb7e9db6af2dbb1cb76a31.png)
[jetcache] how to use jetcache

Rust language - Introduction to Xiaobai 05

思科--高可用和高可靠网络考试
随机推荐
Hide the creation and use of users
Map container
[literacy] deep / shallow, local / global features in machine learning image processing
Lc669. Prune binary search tree
转--拿来即用:分享一个检查内存泄漏的小工具
Share some feelings of a programmer who has experienced layoffs twice a year
MySQL stored procedure
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
# CutefishOS系统~
ECMAScript 2022 正式发布,有你了解过的吗?
分享一个一年经历两次裁员的程序员的一些感触
Mixconv code
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
深度学习--数据操作
Wechat open platform scanning code login [easy to understand]
447 Bili Bili noodles warp 1
第三方验收测试有什么好处?专业第三方软件测试机构推荐
Detailed explanation of common configurations in redis configuration file [easy to understand]
Niuke monthly race - logarithmic sum in groups
cvpr2022 human pose estiamtion