当前位置:网站首页>高攀不起的希尔排序,直接插入排序
高攀不起的希尔排序,直接插入排序
2022-07-01 21:45:00 【老鱼37】
因为期末考试断更了这么久不好意思,从今天起平均每天输出一篇高质量文章供大家享用!!!
First:
直接插入排序:InsertSort
整体思想:
完整代码实现:
//直接插入排序
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);
}
下面介绍的是曾经你对他爱答不理,现在你高攀不起的希尔排序
希尔排序是对直接插入排序的优化
整体思想:
1、预排序---将数据变成快要排序好的数据
步骤:先分小组gap,然后再进行排序
当gap到达1的时候就排序好了
是不是很神奇
总结希尔排序特点:
完整代码实现:
//希尔排序
void ShellSort(int* arr, int len)
{
int gap = len;
while (gap > 1)
{
gap = gap / 3 + 1;//保证最后一个数是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);
}
小伙伴们快点上机操作一下吧!!!
如有错误,请多多指教!
边栏推荐
猜你喜欢
Copy ‘XXXX‘ to effectively final temp variable
News classification based on LSTM model
收到一封CTO来信,邀约面试机器学习工程师
[noip2013] building block competition [noip2018] road laying greed / difference
I received a letter from CTO inviting me to interview machine learning engineer
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Basic operation of binary tree
基于LSTM模型实现新闻分类
按照功能对Boost库进行分类
plantuml介绍与使用
随机推荐
The leader of the cloud native theme group of beacon Committee has a long way to go!
使用闭包实现点击按钮切换 toggle
ICML2022 | 基于元语义正则化的介入性对比学习
What is the difference between PMP and NPDP?
Getting started with the lockust series
Little p weekly Vol.11
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
CSDN购买的课程从哪里可以进入
小 P 周刊 Vol.11
Clean up system cache and free memory under Linux
CNN convolution neural network principle explanation + image recognition application (with source code) [easy to understand]
Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
Separate the letters and numbers in the string so that the letters come first and the array comes last
What is the difference between consonants and Initials? (difference between initials and consonants)
News classification based on LSTM model
PCB plug hole technology~
CNN卷积神经网络原理讲解+图片识别应用(附源码)[通俗易懂]
Using closures to switch toggle by clicking a button
Difference and use between require and import
Airserver mobile phone third-party screen projection computer software