当前位置:网站首页>高攀不起的希尔排序,直接插入排序
高攀不起的希尔排序,直接插入排序
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);
}
小伙伴们快点上机操作一下吧!!!
如有错误,请多多指教!
边栏推荐
- Clean up system cache and free memory under Linux
- Unity 使用Sqlite
- 《QTreeView+QAbstractItemModel自定义模型》:系列教程之三[通俗易懂]
- 【MySQL】explain的基本使用以及各列的作用
- Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
- Talking from mlperf: how to lead the next wave of AI accelerator
- Gaussdb (DWS) active prevention and troubleshooting
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- 【juc学习之路第9天】屏障衍生工具
- linux下清理系统缓存并释放内存
猜你喜欢
Aidl basic use
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
详解ThreadLocal
对象内存布局
MySQL series transaction log redo log learning notes
完全注解的ssm框架搭建
工控设备安全加密的意义和措施
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
比较版本号[双指针截取自己想要的字串]
随机推荐
List announced | outstanding intellectual property service team in China in 2021
Talking from mlperf: how to lead the next wave of AI accelerator
函数基本学习之一
Is PMP certificate really useful?
Basic operation of binary tree
Show member variables and methods in classes in idea
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
An operation tool used by we media professionals who earn 1w+ a month
Internet of things RFID, etc
MIT|256KB 内存下的设备上训练
基础—io密集型计算和cpu密集型计算
MySQL系列之事务日志Redo log学习笔记
One of the basic learning of function
并发编程系列之FutureTask源码学习笔记
What is the difference between PMP and NPDP?
CNN convolution neural network principle explanation + image recognition application (with source code) [easy to understand]
MySQL learning notes - SQL optimization of optimization
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
收到一封CTO来信,邀约面试机器学习工程师