当前位置:网站首页>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 !
边栏推荐
猜你喜欢
el-input文本域字数限制,超过显示变红并禁止输入
思科考试--冗余网络
Metauniverse may become a new direction of Internet development
搜狗微信APP逆向(二)so层
每日刷题记录 (十)
Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
Understanding of indexes in MySQL
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
正则系列之组和范围(Groups and Ranges)
转--利用C语言中的setjmp和longjmp,来实现异常捕获和协程
随机推荐
[daily training] 326 Power of 3
效率提升 - 鼓捣个性化容器开发环境
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
数字货币:影响深远的创新
Tourism Management System
Rust language - Introduction to Xiaobai 05
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
Congratulations on the release of friends' new book (send welfare)
思科--高可用和高可靠网络考试
El input text field word limit, beyond which the display turns red and input is prohibited
Detailed explanation of common configurations in redis configuration file [easy to understand]
Ffmpeg learning notes
Cut noodles C language
下班前几分钟,我弄清了v-model与.sync的区别
Turn -- go deep into Lua scripting language, so that you can thoroughly understand the debugging principle
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received
Flink SQL command line connection yarn
SAP 智能机器人流程自动化(iRPA)解决方案分享